From e7b09151ef9cfe471dbf94bc048f3fcd6784fb9d Mon Sep 17 00:00:00 2001 From: Bodo Junglas Date: Wed, 29 Mar 2017 18:57:59 +0200 Subject: [PATCH] Refactored http error names --- rest/http_error.go | 25 ++++++++++++++++--------- rest/http_error_test.go | 2 +- rest/resource.go | 14 +++++++------- rest/resources.go | 12 ++++++------ rest/rest_handler.go | 4 ++-- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/rest/http_error.go b/rest/http_error.go index 39e3b46..ac3f230 100644 --- a/rest/http_error.go +++ b/rest/http_error.go @@ -60,48 +60,55 @@ func WrapError(err error) *HTTPError { case *HTTPError: return err.(*HTTPError) default: - return InternalServerError(err) + return HTTPInternalServerError(err) } } -// BadRequest is a HTTP bad request 400 -var BadRequest = &HTTPError{ +// HTTPBadRequest is a HTTP bad request 400 +var HTTPBadRequest = &HTTPError{ Code: 400, Type: "https://httpstatus.es/400", Message: "Bad request", } -var UnauthorizedError = &HTTPError{ +// HTTPUnauthorized is a HTTP Unauthorized 401 +var HTTPUnauthorized = &HTTPError{ Code: 401, Type: "https://httpstatus.es/401", Message: "Unauthorized", } -var Forbidden = &HTTPError{ +// HTTPForbidden is a HTTP forbidden 403 +var HTTPForbidden = &HTTPError{ Code: 403, Type: "https://httpstatus.es/403", Message: "Forbidden", } -var NotFound = &HTTPError{ +// HTTPNotFound is a HTTP not found 404 +var HTTPNotFound = &HTTPError{ Code: 404, Type: "https://httpstatus.es/404", Message: "Not found", } -var MethodNotAllowed = &HTTPError{ +// HTTPMethodNotAllowed is a HTTP method not allowed 405 +var HTTPMethodNotAllowed = &HTTPError{ Code: 405, Type: "https://httpstatus.es/405", Message: "Method not allowed", } -var Conflict = &HTTPError{ +// HTTPConflict is a HTTP conflict 409 +var HTTPConflict = &HTTPError{ Code: 409, Type: "https://httpstatus.es/409", Message: "Conflict", } -func InternalServerError(err error) *HTTPError { +// HTTPInternalServerError wraps an arbitrary Error as +// HTTP internal server error 500 +func HTTPInternalServerError(err error) *HTTPError { return &HTTPError{ Code: 500, Type: "https://httpstatus.es/500", diff --git a/rest/http_error_test.go b/rest/http_error_test.go index 838148f..7809a1b 100644 --- a/rest/http_error_test.go +++ b/rest/http_error_test.go @@ -12,7 +12,7 @@ import ( func TestHttpError(t *testing.T) { Convey("Given an InternalServerError", t, func() { - httpErr := rest.InternalServerError(errors.New("Something is wrong")) + httpErr := rest.HTTPInternalServerError(errors.New("Something is wrong")) Convey("When error is send as json", func() { response := httptest.NewRecorder() diff --git a/rest/resource.go b/rest/resource.go index f55f2e4..918caf5 100644 --- a/rest/resource.go +++ b/rest/resource.go @@ -25,27 +25,27 @@ func (ResourceBase) BeforeFilter(resp http.ResponseWriter, req *http.Request) bo } func (ResourceBase) Get(request *http.Request) (interface{}, error) { - return nil, MethodNotAllowed + return nil, HTTPMethodNotAllowed } func (ResourceBase) Post(request *http.Request) (interface{}, error) { - return nil, MethodNotAllowed + return nil, HTTPMethodNotAllowed } func (ResourceBase) Patch(request *http.Request) (interface{}, error) { - return nil, MethodNotAllowed + return nil, HTTPMethodNotAllowed } func (ResourceBase) Update(request *http.Request) (interface{}, error) { - return nil, MethodNotAllowed + return nil, HTTPMethodNotAllowed } func (ResourceBase) Delete(request *http.Request) (interface{}, error) { - return nil, MethodNotAllowed + return nil, HTTPMethodNotAllowed } func (ResourceBase) SubResources() routing.Matcher { - return HttpErrorMatcher(NotFound) + return HTTPErrorMatcher(HTTPNotFound) } type LimitedResource struct { @@ -68,7 +68,7 @@ func ResourceMatcher(resource Resource) routing.Matcher { routing.PUT(restHandler{before: resource.BeforeFilter, handler: resource.Update}), routing.PATCH(restHandler{before: resource.BeforeFilter, handler: resource.Patch}), routing.DELETE(restHandler{before: resource.BeforeFilter, handler: resource.Delete}), - HttpErrorMatcher(MethodNotAllowed), + HTTPErrorMatcher(HTTPMethodNotAllowed), ), resource.SubResources(), ) diff --git a/rest/resources.go b/rest/resources.go index edac8b9..6cfd523 100644 --- a/rest/resources.go +++ b/rest/resources.go @@ -23,15 +23,15 @@ func (ResourcesBase) BeforeFilter(resp http.ResponseWriter, req *http.Request) b } func (ResourcesBase) Create(*http.Request) (Resource, error) { - return nil, MethodNotAllowed + return nil, HTTPMethodNotAllowed } func (ResourcesBase) List(*http.Request) (interface{}, error) { - return nil, MethodNotAllowed + return nil, HTTPMethodNotAllowed } func (ResourcesBase) FindById(id string) (interface{}, error) { - return nil, NotFound + return nil, HTTPNotFound } type LimitedResources struct { @@ -51,7 +51,7 @@ func ResourcesMatcher(prefix string, collection Resources) routing.Matcher { routing.StringPart(func(id string) routing.Matcher { result, err := collection.FindById(id) if err != nil { - return HttpErrorMatcher(WrapError(err)) + return HTTPErrorMatcher(WrapError(err)) } switch resource := (result).(type) { case Resource: @@ -59,13 +59,13 @@ func ResourcesMatcher(prefix string, collection Resources) routing.Matcher { case Resources: return ResourcesMatcher("", resource) default: - return HttpErrorMatcher(InternalServerError(errors.New("Invalid result"))) + return HTTPErrorMatcher(HTTPInternalServerError(errors.New("Invalid result"))) } }), routing.EndSeq( routing.GET(restHandler{before: collection.BeforeFilter, handler: collection.List}), routing.POST(createHandler{before: collection.BeforeFilter, handler: collection.Create}), - HttpErrorMatcher(MethodNotAllowed), + HTTPErrorMatcher(HTTPMethodNotAllowed), ), ) } diff --git a/rest/rest_handler.go b/rest/rest_handler.go index cc1f38f..c992682 100644 --- a/rest/rest_handler.go +++ b/rest/rest_handler.go @@ -17,7 +17,7 @@ func (h restHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { defer func() { if r := recover(); r != nil { fmt.Fprintln(os.Stderr, string(debug.Stack())) - InternalServerError(fmt.Errorf("Paniced: %v", r)).Send(resp, encoder) + HTTPInternalServerError(fmt.Errorf("Paniced: %v", r)).Send(resp, encoder) } }() var err error @@ -48,7 +48,7 @@ func (h createHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { defer func() { if r := recover(); r != nil { fmt.Fprintln(os.Stderr, string(debug.Stack())) - InternalServerError(fmt.Errorf("Paniced: %v", r)).Send(resp, encoder) + HTTPInternalServerError(fmt.Errorf("Paniced: %v", r)).Send(resp, encoder) } }() var err error