Hey, happy new year!
I'm wondering what the go is about error messaging outside of validation, and what can be done in terms of throwing or returning specific errors.
Say you have a User Update Email Mutation, and the email field is unique (classic). There are a couple of things that could go wrong:
Following the example, for user not found it would just return null (which I guess is common for GraphQL I'm still letting go of HTTP status codes). Now for the dupe email, I could also return null, but then the client won't know the difference. If I let the DB take the brunt we'll end up with "Internal Server Error", which is _true_ but then what's the difference between a legit bug and a unique constraint violation... and still the client couldn't inform the user either way.
So I'm just wondering before I spin my wheels on solving this if there's already a format in place for returning specific error types. Also I found that even when APP_ENV=production, exception responses return the whole stack trace, wondering if there's a way to disable this. Thank you very much in advance!
Hey, happy new year!
馃コ
You may not like the/my answer, but: it's up to you!
To give some pointers:
error_formatter to define _how_ the exception/error response looks likeerrors_handler how you want to treat exceptions (wrap them, swap them, ignore them, rethrow a different one)I'm still letting go of HTTP status codes)
At the moment here's no defined _standard_ for this. GraphQL is transport agnostic. Of course HTTP(S) is the most used one. If you're interested, there's WG for that => https://github.com/APIs-guru/graphql-over-http
Also I found that even when
APP_ENV=production, exception responses return the whole stack trace, wondering if there's a way to disable this. Thank you very much in advance!
That sounds a like a bug. However I can't reproduce it, I've projects using this in production and they work correctly. Before filing an issue here, I suggest to review your code and otherwise create a reproducible repository first.
Thanks @mfn, that's a very helpful response!
That sounds a like a bug.
It's being plugged into a legacy codebase, I would assume the bug is on my side. At least I know for sure now what the expected behavior is haha.
Just want to add to this in case someone else comes by : turns out webonyx/graphql-php gives you an interface for custom exceptions.
use GraphQL\Error\ClientAware;
class ResourceNotFoundException extends \Exception implements ClientAware {
protected $message = "Resource not found";
public function isClientSafe() {
return true;
}
public function getCategory() {
return 'resource_not_found';
}
}
When this exception is thrown, the category and message are in the response:
{
"errors": [
{
"message": "Resource not found",
"extensions": {
"category": "resource_not_found"
},
...
}
https://webonyx.github.io/graphql-php/error-handling/
Cheers :-)
Most helpful comment
Just want to add to this in case someone else comes by : turns out webonyx/graphql-php gives you an interface for custom exceptions.
When this exception is thrown, the category and message are in the response:
https://webonyx.github.io/graphql-php/error-handling/
Cheers :-)