Skip to content

Commit

Permalink
swagger: make WithStatus ignore 0s
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Souza committed Feb 22, 2018
1 parent ffa4797 commit 7e47e62
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion swagger/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func NewErrorResponse(err error) *ErrorResponse {

// WithStatus creates a new copy of ErrorResponse using the given status.
func (r *ErrorResponse) WithStatus(status int) *ErrorResponse {
return &ErrorResponse{Message: r.Message, status: status}
if status > 0 {
return &ErrorResponse{Message: r.Message, status: status}
}
return r
}

// Error returns the underlying error message.
Expand Down
15 changes: 15 additions & 0 deletions swagger/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ func TestErrorResponseCustomStatus(t *testing.T) {
}
}

func TestErrorResponseZeroStatus(t *testing.T) {
internalError := errors.New("something went wrong")
errResp := NewErrorResponse(internalError).WithStatus(0)
code, data, err := errResp.Result()
if code != http.StatusInternalServerError {
t.Errorf("Wrong error code. Want %d. Got %d", http.StatusInternalServerError, code)
}
if data != nil {
t.Errorf("Unexpected non-nil data: %#v", data)
}
if err != errResp {
t.Errorf("Wrong error returned. Want %#v. Got %#v", errResp, err)
}
}

func TestErrorResponsErrorInterface(t *testing.T) {
var err error
msg := "something went wrong"
Expand Down

0 comments on commit 7e47e62

Please sign in to comment.