-
Notifications
You must be signed in to change notification settings - Fork 24
Standardize API error return values #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
12f73ca
d5af645
eac493d
e265f92
cdb0619
38ba7e0
1ab1564
9ade91d
adec632
686ddab
53c8967
08e1ab1
026ff5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,16 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "github.com/golang/mock/gomock" | ||
| "github.com/labstack/echo/v4" | ||
| "github.com/stretchr/testify/assert" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "schneider.vip/problem" | ||
| "testing" | ||
| ) | ||
|
|
||
|
|
@@ -102,3 +108,45 @@ func Test_getGroup(t *testing.T) { | |
| assert.Equal(t, "", getGroup("")) | ||
| assert.Equal(t, "", getGroup("/")) | ||
| } | ||
|
|
||
| func TestHttpErrorHandler(t *testing.T) { | ||
| es, _ := createEchoServer(HTTPConfig{}, false) | ||
| e := es.(*echo.Echo) | ||
| server := httptest.NewServer(e) | ||
| client := http.Client{} | ||
|
|
||
| t.Run("Problem return", func(t *testing.T) { | ||
| prb := NewProblem("problem title", http.StatusInternalServerError, "problem detail") | ||
| f := func(c echo.Context) error { | ||
| return prb | ||
| } | ||
| e.Add(http.MethodGet, "/problem", f) | ||
| req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/problem", server.URL), nil) | ||
| resp, err := client.Do(req) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, http.StatusInternalServerError, resp.StatusCode) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test some more. We expect a json struct on the correct content type right? There should be a title and description in here too?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added tests to check content type, and response body. |
||
| assert.Equal(t, problem.ContentTypeJSON, resp.Header.Get("Content-Type")) | ||
|
|
||
| // Validate response body with expected problem | ||
| prbBytes, _ := json.Marshal(prb) | ||
| bodyBytes, err := io.ReadAll(resp.Body) | ||
| if !assert.NoError(t, err) { | ||
| return | ||
| } | ||
| assert.Equal(t, prbBytes, bodyBytes) | ||
| }) | ||
|
|
||
| t.Run("Error return", func(t *testing.T) { | ||
| f := func(c echo.Context) error { | ||
| return errors.New("error") | ||
| } | ||
| e.Add(http.MethodGet, "/error", f) | ||
| req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/error", server.URL), nil) | ||
| resp, err := client.Do(req) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, http.StatusInternalServerError, resp.StatusCode) | ||
| }) | ||
|
|
||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package v1 | ||
|
|
||
| import "schneider.vip/problem" | ||
|
|
||
| // Error is an alias for the internally used problem.Problem | ||
| type Error = problem.Problem |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| description: | | ||
| Default return values follow Problem Details for HTTP APIs as specified in [RFC7807](https://tools.ietf.org/html/rfc7807). | ||
|
|
||
| Currently, return values contain the following members of a problem details object: | ||
| - "title" (string) - A short, human-readable summary of the problem type. | ||
| - "status" (number) - The HTTP status code generated by the origin server for this occurrence of the problem. | ||
| - "detail" (string) - A human-readable explanation specific to this occurrence of the problem. | ||
| content: | ||
| application/problem+json: # https://tools.ietf.org/html/rfc7807#section-6.1 | ||
| schema: | ||
| type: object | ||
| required: | ||
| - title | ||
| - status | ||
| - detail | ||
| properties: | ||
| title: | ||
| type: string | ||
| description: A short, human-readable summary of the problem type. | ||
| status: | ||
| type: number | ||
| description: HTTP statuscode | ||
| detail: | ||
| type: string | ||
| description: A human-readable explanation specific to this occurrence of the problem. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the response already is committed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is nothing we can do if the response is already committed (besides logging). The current approach replicates echo's
DefaultHTTPErrorHandler()