-
-
Notifications
You must be signed in to change notification settings - Fork 568
Provide minimal spec-compliant error format #830
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
Conversation
I agree with you but only in part. Errors are pretty loosely defined in GraphQL and it is probably one of the most obvious areas for improvement in the spec. At the moment everyone deals with errors on their own but I think the spec will eventually come up with some common way to express various categories of errors, error ids, etc. And I would like to avoid the churn of breaking changes until we know the direction of the spec in this regard. |
My motivation for this PR is that we are about to completely revamp error handling in our large application. Our goal is to introduce discrete classes for every single error. Since we do use GraphQL heavily, we want to implement the For existing users of the As to the spec, I found two issues related to error codes:
Both talk about adding a key |
# Conflicts: # docs/error-handling.md
@spawnia @vladar Sorry I missed that discussion. I would love for guidance on this one, I'm only finding about it now as I'm trying to provide feedback to #914, and having to fix our code in the process after the changes in #906 and this one #830 :) I found We have an error handler attached to Our handler keeps the other errors, and replaces all How can this be achieved now please? // in our executor subclass...
if (!Configure::read('debug')) {
$result = $result->setErrorsHandler(new CategoryInterceptorErrorHandler());
} or handler: /**
* Custom GraphQL error handler that will intercept errors of the 'graphql' category
* and replace them with one single error suggesting to reload the client.
*
* The "graphql" category is reserved for errors produced by query parsing or validation.
*/
class CategoryInterceptorErrorHandler
{
/**
* The __invoke() method is called when a script tries to call an object as a function.
*
* @param array $errors Errors.
* @param callable $formatter Formatter.
* @return array of formatted errors.
*/
public function __invoke(array $errors, callable $formatter)
{
$count = count($errors);
$errors = array_filter($errors, static fn ($e) => $e->getCategory() != Error::CATEGORY_GRAPHQL);
if ($count != count($errors)) {
// phpcs:ignore Generic.Files.LineLength
$msg = 'An error occurred while talking to the server. Reloading your client may address the issue. Please contact us if it persists.';
$errors[] = new Error($msg);
}
return array_map($formatter, $errors);
}
} Thanks |
You can examine errors by checking if they:
Implementing |
Thanks @spawnia, this seems to be doing the trick :) |
I strongly believe we should fully comply with http://spec.graphql.org/draft/#sec-Errors.Error-result-format:
If individual services decide to add such entries, that is not even as bad. Given this library is supposed to be a non-opinionated base implemenation of the spec, we absolutely should not.
I also propose to remove the
category
field entirely. It bloats the implementation of theClientAware
interface unnecessarily.As a replacement, i added the
ProvidesExtensions
interface, which allows any error class to add arbitrary additional data to the formatted error.