Skip to content

Commit

Permalink
Merge branch 'v0.16.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
flrdv committed Apr 24, 2024
2 parents 0b08aa8 + 3263f46 commit 8a73543
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
27 changes: 19 additions & 8 deletions http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ func (r *Response) JSON(model any) *Response {
return resp
}

// Error returns Response with corresponding HTTP error code, if passed error is
// status.HTTPError.fields. Otherwise, code is considered to be 500 Internal Server Error.fields.
// Note: revealing error text may be dangerous
func (r *Response) Error(err error) *Response {
// Error returns a response builder with an error set. If passed err is nil, nothing will happen.
// If an instance of status.HTTPError is passed, error code will be automatically set. Custom
// codes can be passed, however only first will be used. By default, the error is
// status.ErrInternalServerError
func (r *Response) Error(err error, code ...status.Code) *Response {
if err == nil {
return r
}
Expand All @@ -206,8 +207,14 @@ func (r *Response) Error(err error) *Response {
return r.Code(http.Code)
}

c := status.InternalServerError
if len(code) > 0 {
// peek the first, ignore rest
c = code[0]
}

return r.
Code(status.InternalServerError).
Code(c).
String(err.Error())
}

Expand All @@ -219,7 +226,6 @@ func (r *Response) Reveal() response.Fields {
// Clear discards everything was done with Response object before
func (r *Response) Clear() *Response {
r.fields = r.fields.Clear()

return r
}

Expand Down Expand Up @@ -254,6 +260,11 @@ func JSON(request *Request, model any) *Response {
}

// Error is a predicate to request.Respond().Error(...)
func Error(request *Request, err error) *Response {
return request.Respond().Error(err)
//
// Error returns a response builder with an error set. If passed err is nil, nothing will happen.
// If an instance of status.HTTPError is passed, error code will be automatically set. Custom
// codes can be passed, however only first will be used. By default, the error is
// status.ErrInternalServerError
func Error(request *Request, err error, code ...status.Code) *Response {
return request.Respond().Error(err, code...)
}
17 changes: 17 additions & 0 deletions indigo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func getInbuiltRouter() *inbuilt.Router {
return http.JSON(request, headersToMap(request.Headers, fields))
})

r.Get("/custom-error-with-code", func(request *http.Request) *http.Response {
return http.Error(request, status.ErrTeapot, status.Teapot)
})

return r
}

Expand Down Expand Up @@ -256,6 +260,19 @@ func TestFirstPhase(t *testing.T) {
require.Empty(t, body)
})

t.Run("error with custom code", func(t *testing.T) {
resp, err := stdhttp.DefaultClient.Get(appURL + "/custom-error-with-code")
require.NoError(t, err)
defer func() {
_ = resp.Body.Close()
}()

require.Equal(t, stdhttp.StatusTeapot, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Empty(t, body)
})

t.Run("with query", func(t *testing.T) {
resp, err := stdhttp.DefaultClient.Get(appURL + "/query?hello=world&%20foo=+bar")
require.NoError(t, err)
Expand Down

0 comments on commit 8a73543

Please sign in to comment.