Skip to content

Commit

Permalink
Refactored http error names
Browse files Browse the repository at this point in the history
  • Loading branch information
untoldwind committed Mar 29, 2017
1 parent 8a64785 commit e7b0915
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
25 changes: 16 additions & 9 deletions rest/http_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion rest/http_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 7 additions & 7 deletions rest/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
)
Expand Down
12 changes: 6 additions & 6 deletions rest/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -51,21 +51,21 @@ 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:
return ResourceMatcher(resource)
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),
),
)
}
4 changes: 2 additions & 2 deletions rest/rest_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e7b0915

Please sign in to comment.