Skip to content

Conversation

@ollietb
Copy link
Contributor

@ollietb ollietb commented Jul 7, 2020

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tickets N/A
License MIT
Doc PR api-platform/docs#1142

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.

@ollietb
Copy link
Contributor Author

ollietb commented Jul 7, 2020

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;
Copy link
Member

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);
Copy link
Member

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.

@alanpoulain
Copy link
Member

Thanks for this PR!

I think the interface could be registered for autoconfiguration (in ApiPlatformExtension).

I've added some (minor) comments.
Could you fix the CS too?
You can also add a CHANGELOG entry and a PR for the documentation (it's not mandatory, I will do it myself otherwise).

@alanpoulain alanpoulain merged commit 0be9798 into api-platform:master Jul 18, 2020
@alanpoulain
Copy link
Member

Thanks a lot @ollietb!

@ollietb
Copy link
Contributor Author

ollietb commented Jul 21, 2020

@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.

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.

2 participants