Skip to content

Commit

Permalink
Added ResponseErrorWriter interface to allow for custom error json ma…
Browse files Browse the repository at this point in the history
…rshalling
  • Loading branch information
Keith Ball committed Jul 27, 2015
1 parent 80565de commit c0f87c7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
10 changes: 4 additions & 6 deletions method_not_allowed.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,14 @@ func (h MethodNotAllowedHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
fmt.Fprint(w, strings.Join(methods, ", "))
}
} else {
description := fmt.Sprintf(
methodNotAllowedErr := MethodNotAllowed{Err: errors.New(fmt.Sprintf(
"only %s are allowed",
strings.Join(methods, ", "),
)
))}
if acceptJSON(r) {
ResponseErrorWriter.WriteJSONError(w, MethodNotAllowed{Err: errors.New(description)})
ResponseErrorWriter.WriteJSONError(w, methodNotAllowedErr)
} else {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprint(w, description)
ResponseErrorWriter.WritePlaintextError(w, methodNotAllowedErr)
}
}
}
8 changes: 3 additions & 5 deletions not_found.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import (
type NotFoundHandler struct{}

func (NotFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
description := fmt.Sprintf("%s %s not found", r.Method, r.URL.Path)
notFoundErr := NotFound{Err: errors.New(fmt.Sprintf("%s %s not found", r.Method, r.URL.Path))}
if acceptJSON(r) {
ResponseErrorWriter.WriteJSONError(w, NotFound{Err: errors.New(description)})
ResponseErrorWriter.WriteJSONError(w, notFoundErr)
} else {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, description)
ResponseErrorWriter.WritePlaintextError(w, notFoundErr)
}
}

0 comments on commit c0f87c7

Please sign in to comment.