Skip to content

Standardize API error return values#183

Merged
gerardsn merged 13 commits into
masterfrom
iss#009-api-return-values
Apr 1, 2021
Merged

Standardize API error return values#183
gerardsn merged 13 commits into
masterfrom
iss#009-api-return-values

Conversation

@gerardsn

@gerardsn gerardsn commented Mar 24, 2021

Copy link
Copy Markdown
Member

Adds general error handeling middleware.
Proposes standard for 400+ return values. Currently only for crypto, the rest is tracked in #112.
Closes #3 #9

@codecov

codecov Bot commented Mar 24, 2021

Copy link
Copy Markdown

Codecov Report

Merging #183 (026ff5f) into master (d6a01d2) will decrease coverage by 0.01%.
The diff coverage is 86.66%.

Impacted Files Coverage Δ
crypto/api/v1/api.go 83.33% <75.00%> (ø)
core/echo.go 93.10% <88.88%> (-1.90%) ⬇️
core/engine.go 94.33% <100.00%> (-0.90%) ⬇️

@gerardsn
gerardsn force-pushed the iss#009-api-return-values branch from 5db043f to fb1cafa Compare March 24, 2021 09:56
@gerardsn gerardsn changed the title Iss#009 api return values Standardize API error return values Mar 24, 2021
@gerardsn

Copy link
Copy Markdown
Member Author

Increase max-width of read the docs 800 -> 1000px
Changed font swagger to 12px

@gerardsn
gerardsn marked this pull request as ready for review March 24, 2021 10:22

@reinkrul reinkrul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the setup. Some small things.

Comment thread core/echo.go
func httpErrorHandler(err error, ctx echo.Context) {
if prb, ok := err.(*problem.Problem); ok {
// todo: connect to module logger
if !ctx.Response().Committed {

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member Author

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()

Comment thread core/echo.go Outdated
// todo: connect to module logger
if !ctx.Response().Committed {
if _, err := prb.WriteTo(ctx.Response()); err != nil {
//ctx.Echo().Logger.Error(err)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing something?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if the write fails you want to see it in the logs.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failing to write a response is unlikely, so we can log this in the core logger

Comment thread crypto/api/v1/api.go Outdated
if err != nil {
log.Logger().Error(err.Error())
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
return createProblem("SignJWT failed", http.StatusBadRequest, err.Error())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make consts of these titles

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

Comment thread crypto/api/v1/api.go Outdated
Comment thread crypto/api/v1/api.go Outdated

@stevenvegt stevenvegt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice first PR. Some minor points to fix 👍

Comment thread core/echo.go Outdated
return ""
}

func creatorFn() EchoServer {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is a bit generic.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to createEchoServer

Comment thread crypto/api/v1/api_test.go Outdated
Comment thread crypto/api/v1/api_test.go Outdated
Comment thread core/test.go Outdated

// ErrorToProblem returns a Problem generated from a problem.Problem
// problem.Problem doesn't expose its fields
func ErrorToProblem(err error) Problem {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like these poluting the global namespace. The problem can perhaps be made private?
Or a few helper methods:

  • TestAssertProblemErrStatus(err error, status int) bool
  • TestAssertProblemErrTitle(err error, title string) bool
  • TestAssertProblemErrMessage(err error, message string) bool

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have been moved to nuts-node/test. Since the conversion from from error to problem.Probelm now takes place within the test method, I've added a check to make sure the error actually is a problem before verifying the field's specified value.

Comment thread core/echo.go Outdated
// todo: connect to module logger
if !ctx.Response().Committed {
if _, err := prb.WriteTo(ctx.Response()); err != nil {
//ctx.Echo().Logger.Error(err)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if the write fails you want to see it in the logs.

Comment thread core/echo_test.go
resp, err := client.Do(req)

assert.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?

@gerardsn gerardsn Mar 29, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added tests to check content type, and response body.

Comment thread core/test.go
@gerardsn
gerardsn requested review from reinkrul and stevenvegt March 29, 2021 19:24
@gerardsn
gerardsn marked this pull request as draft March 30, 2021 08:53
@gerardsn
gerardsn force-pushed the iss#009-api-return-values branch from c993a5f to 08e1ab1 Compare March 30, 2021 14:54
@gerardsn
gerardsn marked this pull request as ready for review March 30, 2021 15:03

@stevenvegt stevenvegt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvements. 2 minor things / questions.

Comment thread core/echo_test.go Outdated
assert.Equal(t, problem.ContentTypeJSON, resp.Header.Get("Content-Type"))

// Validate response body with expected problem
prb := NewProblem("problem title", http.StatusInternalServerError, "problem detail")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you already created this problem on line 120 right?

Comment thread test/problem.go Outdated
}

// AssertErrProblemTitle asserts err is a *problem.Problem with the specified title
func AssertErrProblemTitle(t assert.TestingT, title string, err error) bool {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the use of asssert.TestingT instead of golang's testing.T?

@woutslakhorst
woutslakhorst dismissed stale reviews from stevenvegt and reinkrul April 1, 2021 08:38

addressed as requested

@gerardsn
gerardsn merged commit c6768d8 into master Apr 1, 2021
@gerardsn
gerardsn deleted the iss#009-api-return-values branch April 1, 2021 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add error handling middleware to echo server

4 participants