fix: some http status error codes are not sent to client du to order

This commit is contained in:
HeshamTB 2024-03-28 21:56:36 +03:00
parent 7d1a0cdbdc
commit 64880261c2
2 changed files with 3 additions and 2 deletions

View File

@ -65,7 +65,7 @@ func run(ctx *cli.Context) {
apiMux.HandleFunc("POST /peer", hvpnnode3.HandlePostPeer(wgLink)) apiMux.HandleFunc("POST /peer", hvpnnode3.HandlePostPeer(wgLink))
apiMux.HandleFunc("DELETE /peer/{pubkey}", hvpnnode3.HandleDeletePeer(wgLink)) apiMux.HandleFunc("DELETE /peer/{pubkey}", hvpnnode3.HandleDeletePeer(wgLink))
apiMux.HandleFunc("GET /peers", hvpnnode3.HandleGetPeers(wgLink)) apiMux.HandleFunc("GET /peers", hvpnnode3.HandleGetPeers(wgLink))
apiMux.HandleFunc("GET /peer/new", hvpnnode3.HandleGetCreatePeer(wgLink)) apiMux.HandleFunc("GET /peer", hvpnnode3.HandleGetCreatePeer(wgLink))
var handler http.Handler = apiMux var handler http.Handler = apiMux
handler = hvpnnode3.HttpAuthToken(handler, ctx.String("http-api-key")) handler = hvpnnode3.HttpAuthToken(handler, ctx.String("http-api-key"))

View File

@ -52,12 +52,13 @@ func HandleGetPeer(wgLink *WGLink) http.HandlerFunc {
peer, err := wgLink.GetPeer(pubkey) peer, err := wgLink.GetPeer(pubkey)
if err != nil { if err != nil {
if errors.Is(err, proto.PeerDoesNotExist){ if errors.Is(err, proto.PeerDoesNotExist){
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode( json.NewEncoder(w).Encode(
proto.ErrJSONResponse{Message: "Peer does not exist"}, proto.ErrJSONResponse{Message: "Peer does not exist"},
) )
w.WriteHeader(http.StatusNotFound)
return return
} }
slog.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }