Skip to content

Commit

Permalink
fix: don't include error in response
Browse files Browse the repository at this point in the history
Also make sure we log errors related to HTTP processing.

closes #2162
  • Loading branch information
stuartwdouglas committed Jul 26, 2024
1 parent 26e7d83 commit efcc46f
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions backend/controller/ingress/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ func Handle(
http.NotFound(w, r)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Errorf(err, "failed to resolve route for %s %s", r.Method, r.URL.Path)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

body, err := BuildRequestBody(route, r, sch)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
// Only log at debug, as this is a client side error
logger.Debugf("bad request: %s", err.Error())
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}

Expand All @@ -53,10 +56,12 @@ func Handle(

resp, err := call(r.Context(), creq, optional.Some(requestKey), r.RemoteAddr)
if err != nil {
logger.Errorf(err, "failed to call verb %s", route.Verb)
if connectErr := new(connect.Error); errors.As(err, &connectErr) {
http.Error(w, err.Error(), connectCodeToHTTP(connectErr.Code()))
httpCode := connectCodeToHTTP(connectErr.Code())
http.Error(w, http.StatusText(httpCode), httpCode)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
return
}
Expand All @@ -65,22 +70,25 @@ func Handle(
verb := &schema.Verb{}
err = sch.ResolveToType(&schema.Ref{Name: route.Verb, Module: route.Module}, verb)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Errorf(err, "could not resolve schema type for verb %s", route.Verb)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
var responseBody []byte

if metadata, ok := verb.GetMetadataIngress().Get(); ok && metadata.Type == "http" {
var response HTTPResponse
if err := json.Unmarshal(msg.Body, &response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Errorf(err, "could not unmarhal response for verb %s", verb)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

var responseHeaders http.Header
responseBody, responseHeaders, err = ResponseForVerb(sch, verb, response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Errorf(err, "could not create response for verb %s", verb)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

Expand All @@ -102,7 +110,7 @@ func Handle(
}

case *ftlv1.CallResponse_Error_:
http.Error(w, msg.Error.Message, http.StatusInternalServerError)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}

Expand Down

0 comments on commit efcc46f

Please sign in to comment.