Skip to content

Commit

Permalink
fix: Propagate dev server errors in CLI compatible format (#412)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mike-zorn authored Aug 26, 2024
1 parent 66a6a25 commit 67cc93a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
39 changes: 39 additions & 0 deletions internal/dev_server/api/error_handlers.go
Original file line number Diff line number Diff line change
@@ -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
13 changes: 2 additions & 11 deletions internal/dev_server/dev_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 {
Expand Down

0 comments on commit 67cc93a

Please sign in to comment.