Skip to content

Log graphQL Errors from result #379

@mauriau

Description

@mauriau
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Version/Branch 0.11

somethings like this
I will make the PR

<?php

declare(strict_types=1);

namespace Overblog\GraphQLBundle\EventListener;

use Overblog\GraphQLBundle\Error\ErrorHandler;
use Overblog\GraphQLBundle\Event\ExecutorResultEvent;
use Psr\Log\LoggerInterface;

final class ErrorHandlerListener
{
    /** @var ErrorHandler */
    private $errorHandler;

    /** @var bool */
    private $throwException;

    /** @var bool */
    private $debug;
    
    /** @var LoggerInterface */
    private $logger;

    public function __construct(
        ErrorHandler $errorHandler,
        LoggerInterface $logger,
        bool $throwException = false,
        bool $debug = false
    ) {
        $this->errorHandler = $errorHandler;
        $this->throwException = $throwException;
        $this->debug = $debug;
        $this->logger = $logger;
    }

    public function onPostExecutor(ExecutorResultEvent $executorResultEvent): void
    {
        $result = $executorResultEvent->getResult();
        $this->errorHandler->handleErrors($result, $this->throwException, $this->debug);
        $result = $result->toArray();

        if (isset($result['errors'])) {
            $this->logger->error(__METHOD__.' : '.\json_encode($result['errors']));
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions