-
-
Notifications
You must be signed in to change notification settings - Fork 249
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 errors to send an error code in the response extension #246
Conversation
@DJLemkes thanks for volunteering on this! I think the idea of using a trait extending I had another approach in mind, let me explain it and you can tell me what you think. If we added Tip: run |
@ghostdogpr Like your idea better! Will give it a shot in the next couple of days. |
d060434
to
d68458e
Compare
@ghostdogpr sorry, had the flue hence the delay. Could you have a look and tell me if this is what you had in mind? |
@DJLemkes no problem, I'll have a closer look tomorrow! |
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.
Looking good! Just have some minor remarks.
Json | ||
.obj( | ||
"message" -> s"Parsing Error: $msg".asJson, | ||
"locations" -> Some(locationInfo).collect { | ||
case Some(li) => Json.arr(locationToJson(li)) | ||
}.asJson | ||
}.asJson, | ||
"extensions" -> extensions.asInstanceOf[Option[ResponseValue]].asJson.dropNullValues |
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.
(extensions: Option[ResponseValue])
would be better than asInstanceOf
(here and below)
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.
Makes a lot of sense but I can't seem to get the encoder for an Option[ResponseValue]
in implicit scope such that .asJson
works. Thought that the implicit circeEncoder
of ResponseValue
would be able to pick this one up? I got the .asInstanceOf[ResponseValue]
trick from GraphQLResponse
actually.
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.
(extensions: Option[ResponseValue]).asJson.dropNullValues
doesn't work? We were about to remove the asInstanceOf
in this PR by using that same trick: https://github.com/ghostdogpr/caliban/pull/234/files#diff-16ab822f0162e843af921d1e9d0c34dfR23
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.
Aha, I was trying to put it in the match clause. Apparently didn't fully understand what you meant. Changed it now and it works!
d68458e
to
b9ca4b5
Compare
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.
Awesome!
Json | ||
.obj( | ||
"message" -> s"Parsing Error: $msg".asJson, | ||
"locations" -> Some(locationInfo).collect { | ||
case Some(li) => Json.arr(locationToJson(li)) | ||
}.asJson | ||
}.asJson, | ||
"extensions" -> extensions.asInstanceOf[Option[ResponseValue]].asJson.dropNullValues |
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.
(extensions: Option[ResponseValue]).asJson.dropNullValues
doesn't work? We were about to remove the asInstanceOf
in this PR by using that same trick: https://github.com/ghostdogpr/caliban/pull/234/files#diff-16ab822f0162e843af921d1e9d0c34dfR23
vuepress/docs/docs/middleware.md
Outdated
|
||
## Customizing error responses | ||
|
||
During various phases of executing a query, an error may occur. Caliban renders the different instances of `CalibanError` to a GraphQL spec compliant response. As a user, you will most likely encounter `ExecutionError` at some point because this will encapsulate the errors in the error channel of your effects. For Caliban to be able to render some basic message about the error that occured during query execution, it is important that your errror extends `Throwable`. |
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.
typo: errror
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.
fixed.
|
||
During various phases of executing a query, an error may occur. Caliban renders the different instances of `CalibanError` to a GraphQL spec compliant response. As a user, you will most likely encounter `ExecutionError` at some point because this will encapsulate the errors in the error channel of your effects. For Caliban to be able to render some basic message about the error that occured during query execution, it is important that your errror extends `Throwable`. | ||
|
||
For more meaningful error handling, GraphQL spec allows for an [`extension`](http://spec.graphql.org/June2018/#example-fce18) object in the error response. This object may include, for instance, `code` information to model enum-like error codes that can be handled by a front-end. In order to generate this information, one can use the `mapError` function on a `GraphQLInterpreter`. An example is provided below in which we map a custom domain error within an `ExecutionError` to a meaningful error code. |
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.
Nice doc!
@DJLemkes thanks for your contribution! |
@ghostdogpr Thanks for you reviews and patience! And for creating the library of course :) |
A first attempt to allow errors to define an error code which is renderd in the
extensions
section of error elements. Closely related to #158I have no idea whether this is solution is on the right track. If not, no hard feelings on rejections :). If it is on the right track, I can put in a little more effort to include more error information as mentioned in #158
Closes #158