From 67cc93a6b3129956b228fe1d1506d277abc3b0b1 Mon Sep 17 00:00:00 2001 From: Mike Zorn Date: Mon, 26 Aug 2024 10:07:21 -0700 Subject: [PATCH] fix: Propagate dev server errors in CLI compatible format (#412) This makes it so that errors from the dev server are propagated back to the CLI in a way that the CLI can reasonably render. This way, we can return something that gives the user an idea of what went wrong vs. a generic "unexpected error" message. --- internal/dev_server/api/error_handlers.go | 39 +++++++++++++++++++++++ internal/dev_server/dev_server.go | 13 ++------ 2 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 internal/dev_server/api/error_handlers.go diff --git a/internal/dev_server/api/error_handlers.go b/internal/dev_server/api/error_handlers.go new file mode 100644 index 00000000..d36bfcdb --- /dev/null +++ b/internal/dev_server/api/error_handlers.go @@ -0,0 +1,39 @@ +package api + +import ( + "encoding/json" + "log" + "net/http" +) + +type errorHandler struct { + code string + statusCode int +} + +func (eh errorHandler) HandleError(w http.ResponseWriter, r *http.Request, err error) { + log.Printf("Error while handling request: %+v", err) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(eh.statusCode) + err = json.NewEncoder(w).Encode(ErrorResponseJSONResponse{ + Code: eh.code, + Message: err.Error(), + }) + if err != nil { + log.Printf("Error while writing error response: %+v", err) + } +} + +var RequestErrorHandler = errorHandler{ + // HACK: This is really just repeating the status code. + // It'd be nice to make these be codes that are meaningful to the user. + code: "bad_request", + statusCode: http.StatusBadRequest, +}.HandleError + +var ResponseErrorHandler = errorHandler{ + // HACK: This is really just repeating the status code. + // It'd be nice to make these be codes that are meaningful to the user. + code: "internal_server_error", + statusCode: http.StatusInternalServerError, +}.HandleError diff --git a/internal/dev_server/dev_server.go b/internal/dev_server/dev_server.go index 223689e0..ae820f5b 100644 --- a/internal/dev_server/dev_server.go +++ b/internal/dev_server/dev_server.go @@ -50,8 +50,8 @@ func (c LDClient) RunServer(ctx context.Context, serverParams ServerParams) { } ss := api.NewStrictServer() apiServer := api.NewStrictHandlerWithOptions(ss, nil, api.StrictHTTPServerOptions{ - RequestErrorHandlerFunc: RequestErrorHandler, - ResponseErrorHandlerFunc: ResponseErrorHandler, + RequestErrorHandlerFunc: api.RequestErrorHandler, + ResponseErrorHandlerFunc: api.ResponseErrorHandler, }) r := mux.NewRouter() r.Use(adapters.Middleware(*ldClient, serverParams.DevStreamURI)) @@ -75,15 +75,6 @@ func (c LDClient) RunServer(ctx context.Context, serverParams ServerParams) { log.Fatal(server.ListenAndServe()) } -func ResponseErrorHandler(w http.ResponseWriter, r *http.Request, err error) { - log.Printf("Error while serving response: %+v", err) - http.Error(w, err.Error(), http.StatusInternalServerError) -} -func RequestErrorHandler(w http.ResponseWriter, r *http.Request, err error) { - log.Printf("Error while serving request: %+v", err) - http.Error(w, err.Error(), http.StatusBadRequest) -} - func getDBPath() string { dbFilePath, err := xdg.StateFile("ldcli/dev_server.db") if err != nil {