Skip to content

Commit

Permalink
whjson: fix error status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jtolio committed Feb 21, 2017
1 parent 986a5e3 commit 7559cf8
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions whjson/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ var (
ErrHandler = wherr.HandlerFunc(errHandler)
)

func errHandler(w http.ResponseWriter, r *http.Request, err error) {
log.Printf("error: %v", err)
func errHandler(w http.ResponseWriter, r *http.Request, handledErr error) {
log.Printf("error: %v", handledErr)
data, err := json.MarshalIndent(map[string]string{
"err": errhttp.GetErrorBody(err)}, "", " ")
"err": errhttp.GetErrorBody(handledErr)}, "", " ")
if err != nil {
log.Printf("failed serializing error: %v", err)
log.Printf("failed serializing error: %v", handledErr)
data = []byte(`{"err": "Internal Server Error"}`)
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", fmt.Sprint(len(data)))
w.WriteHeader(errhttp.GetStatusCode(err, http.StatusInternalServerError))
w.WriteHeader(errhttp.GetStatusCode(handledErr,
http.StatusInternalServerError))
w.Write(data)
}

Expand Down

0 comments on commit 7559cf8

Please sign in to comment.