Skip to content
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

Allow customizing http and websocket status codes for errors #978

Merged
merged 3 commits into from Jan 15, 2020

Conversation

vektah
Copy link
Collaborator

@vektah vektah commented Jan 7, 2020

This PR sets a general rule that:

  • parse and validation errors are 422s
  • resolver errors are 200s
  • anything else is a 200.

same applies to websockets, except they use GQL_ERROR and GQL_DATA message types instead of status codes.

But we need a way to distinguish between the types of errors. Apollo sets a "code" extension, lets copy them and match their codes. I've included the two core ones in the errcode package, you can return anything and they will be treated as 200s by default. To register a custom 422:

func init()  {
   errcode.Register("MY_CODE", errcode.KindProtocol) // make this code a 422 
}
// when you return an error:
errcode.Set(err, "MY_CODE")

I've also added integration tests for websockets and fixes a bunch of websocket related issues, mostly around error handling.

closes #948
closes #965

I have:

  • Added tests covering the bug / feature (see testing)
  • Updated any relevant documentation (see docs)

Copy link
Collaborator

@vvakame vvakame left a comment

Choose a reason for hiding this comment

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

Hi! I'm back from holiday today. sorry for late response 😉
I think this PR is nice than my PR 👍

graphql/handler/transport/websocket.go Outdated Show resolved Hide resolved
graphql/handler/transport/websocket.go Outdated Show resolved Hide resolved
graphql/handler/transport/websocket.go Outdated Show resolved Hide resolved
graphql/handler/transport/websocket_test.go Outdated Show resolved Hide resolved
integration/readme.md Outdated Show resolved Hide resolved
graphql/errcode/codes.go Show resolved Hide resolved
@vektah vektah merged commit ae79e75 into master Jan 15, 2020
@vektah vektah deleted the pluggable-error-code branch January 15, 2020 05:49
cgxxv pushed a commit to cgxxv/gqlgen that referenced this pull request Mar 25, 2022
Allow customizing http and websocket status codes for errors
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.

None yet

2 participants