fix(graphql): dont report client errors (#961)

This commit is contained in:
Kyrch
2025-09-13 17:12:48 -03:00
committed by GitHub
parent 4241c6ed5e
commit bc33705162
2 changed files with 49 additions and 1 deletions
+48
View File
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace App\GraphQL\Handler;
use Error as PhpError;
use Exception;
use GraphQL\Error\Error as GraphQLError;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Rebing\GraphQL\Error\AuthorizationError;
use Rebing\GraphQL\Error\ValidationError;
class ErrorHandler
{
public static function handleErrors(array $errors, callable $formatter): array
{
$handler = app()->make(ExceptionHandler::class);
foreach ($errors as $error) {
// Try to unwrap exception
$error = $error->getPrevious() ?: $error;
// Don't report certain GraphQL errors
if ($error instanceof ValidationError ||
$error instanceof AuthorizationError ||
$error instanceof GraphQLError ||
! (
$error instanceof Exception ||
$error instanceof PhpError
)) {
continue;
}
if (! $error instanceof Exception) {
$error = new Exception(
$error->getMessage(),
$error->getCode(),
$error
);
}
$handler->report($error);
}
return array_map($formatter, $errors);
}
}
+1 -1
View File
@@ -98,7 +98,7 @@ return [
*
* The default handler will pass exceptions to laravel Error Handling mechanism
*/
'errors_handler' => [Rebing\GraphQL\GraphQL::class, 'handleErrors'],
'errors_handler' => [App\GraphQL\Handler\ErrorHandler::class, 'handleErrors'],
/*
* Options to limit the query complexity and depth. See the doc