-
-
Notifications
You must be signed in to change notification settings - Fork 940
Handling GraphQL errors #3632
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
Handling GraphQL errors #3632
Conversation
|
This allows for a decorator to be created to handle the errors, eg <?php
namespace App\GraphQL\Error;
use ApiPlatform\Core\GraphQl\Error\ErrorHandlerInterface;
use Psr\Log\LoggerInterface;
class ErrorHandler implements ErrorHandlerInterface
{
/**
* @var ErrorHandlerInterface
*/
private ErrorHandlerInterface $errorHandler;
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
public function __construct(ErrorHandlerInterface $errorHandler, LoggerInterface $logger)
{
$this->errorHandler = $errorHandler;
$this->logger = $logger;
}
public function __invoke(array $errors, callable $formatter)
{
foreach ($errors as $error) {
$this->logger->error('GraphQL Exception', array('exception' => $error));
}
$errorHandler = $this->errorHandler;
return $errorHandler($errors, $formatter);
}
} |
| $this->graphiqlEnabled = $graphiqlEnabled; | ||
| $this->graphQlPlaygroundEnabled = $graphQlPlaygroundEnabled; | ||
| $this->defaultIde = $defaultIde; | ||
| $this->errorHandler = $errorHandler; |
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.
Please add it before $this->debug.
|
|
||
| interface ErrorHandlerInterface | ||
| { | ||
| public function __invoke(array $errors, callable $formatter); |
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.
I think the return type could be added as well.
|
Thanks for this PR! I think the interface could be registered for autoconfiguration (in I've added some (minor) comments. |
|
Thanks a lot @ollietb! |
|
@alanpoulain sorry I've just got back from vacation and in my testing I'm not sure the autowiring is working correctly. tbh I'm not really sure how to set it up. I've been using the decorator which works ok. |
Porting the functionality from the 2.5.x branch over to master - replaces existing PR #3630
Errors thrown from the platform are caught by GraphQL and can't be logged or tracked in production environments, making it impossible to track down the cause of errors or use error analysers. This PR allows the errors from GraphQL to be handled by the developers by decorating the ErrorHandler. The errors can either logged (though a service like monolog) or sent to a 3rd party error analyser, like Sentry or NewRelic.