From c0f87c7a72c2b362a658eda63d7c86ef73e10b0b Mon Sep 17 00:00:00 2001 From: Keith Ball Date: Mon, 27 Jul 2015 10:18:38 +0200 Subject: [PATCH] Added ResponseErrorWriter interface to allow for custom error json marshalling --- method_not_allowed.go | 10 ++++------ not_found.go | 8 +++----- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/method_not_allowed.go b/method_not_allowed.go index 809422b..2bfc181 100644 --- a/method_not_allowed.go +++ b/method_not_allowed.go @@ -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) } } } diff --git a/not_found.go b/not_found.go index 5d4ffb1..0b8ca0e 100644 --- a/not_found.go +++ b/not_found.go @@ -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) } }