Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Propagate dev server errors in CLI compatible format #412

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Code seems like a strange name for this. It looks like statusText.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tragically, this is the convention in our API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I figured there was a reason for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment to indicate that this is not ideal.

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
Loading