-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
41 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters