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. I've found this section of the docs: 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 MyCustomErrorHandler implements 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 other 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!