简体   繁体   中英

Log Laravel Lighthouse Graphql errors

I currently have a Laravel 8.49.0 project with a GraphQL api made with Lighthouse

The thing is that I want to log all the errors that it can throw. For that, I've found at the docs this: Error Handling

I've modified the lighthouse.php config file to use my custom ErrorHandler:

 'error_handlers' => [
        //\Nuwave\Lighthouse\Execution\ExtensionErrorHandler::class,
        //\Nuwave\Lighthouse\Execution\ReportingErrorHandler::class,
        \App\GraphQL\ErrorHandlers\MyCustomErrorHandler::class,
    ],

The class is the one that they were using adding the line to log the error:

<?php

namespace App\GraphQL\ErrorHandlers;

use Closure;
use GraphQL\Error\Error;
use Illuminate\Support\Facades\Log;
use Nuwave\Lighthouse\Execution\ErrorHandler;
use Nuwave\Lighthouse\Exceptions\RendersErrorsExtensions;

/**
 * Handle Exceptions that implement Nuwave\Lighthouse\Exceptions\RendersErrorsExtensions
 * and add extra content from them to the 'extensions' key of the Error that is rendered
 * to the User.
 */
class MyCustomErrorHandlerimplements ErrorHandler
{
    public static function handle(Error $error, Closure $next): array
    {
        Log::error('GRAPHQL Error: '.$error->message);
        $underlyingException = $error->getPrevious();

        if ($underlyingException instanceof RendersErrorsExtensions) {
            // Reconstruct the error, passing in the extensions of the underlying exception
            $error = new Error( // @phpstan-ignore-line TODO remove after graphql-php upgrade
                $error->message,
                $error->nodes,
                $error->getSource(),
                $error->getPositions(),
                $error->getPath(),
                $underlyingException,
                $underlyingException->extensionsContent()
            );
        }

        return $next($error);
    }
}

Some errors are logging correctly like these ones, even with the problem with the memory:

在此处输入图像描述

在此处输入图像描述

But the problem is that another queries are throwing errors that are not getting registered like this one:

在此处输入图像描述

I don't want to solve the error, I want it to be registered on my log like you can see in the picture above.

What can I do to get all the errors on my log? I'm a bit lost with this. Don't know if json errors are treated diferently.

Thanks in advance and sorry for my english!

Your registered error handler only catches errors that happen during GraphQL execution. It is possible for error to happen outside of that, for example during initial handling of the HTTP request or while sending the GraphQL response.

Try adding logging to your app/Exceptions/Handler.php .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM