Skip to content

fix(laravel): decorate error handler #7247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Laravel/ApiPlatformDeferredProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
use ApiPlatform\State\Provider\ParameterProvider;
use ApiPlatform\State\Provider\SecurityParameterProvider;
use ApiPlatform\State\ProviderInterface;
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerInterface;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -252,9 +252,9 @@ public function register(): void
);
});

$this->app->singleton(
ExceptionHandlerInterface::class,
function (Application $app) {
$this->app->extend(
ExceptionHandler::class,
function (ExceptionHandler $decorated, Application $app) {
/** @var ConfigRepository */
$config = $app['config'];

Expand All @@ -267,7 +267,8 @@ function (Application $app) {
$app->make(Negotiator::class),
$config->get('api-platform.exception_to_status'),
$config->get('app.debug'),
$config->get('api-platform.error_formats')
$config->get('api-platform.error_formats'),
$decorated
);
}
);
Expand Down
3 changes: 1 addition & 2 deletions src/Laravel/Eloquent/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
use ApiPlatform\State\Pagination\HasNextPagePaginatorInterface;
use ApiPlatform\State\Pagination\PaginatorInterface;
use Illuminate\Pagination\LengthAwarePaginator;
use IteratorAggregate;

/**
* @implements IteratorAggregate<mixed,object>
* @implements \IteratorAggregate<mixed,object>
* @implements PaginatorInterface<object>
*/
final class Paginator implements PaginatorInterface, HasNextPagePaginatorInterface, \IteratorAggregate
Expand Down
3 changes: 1 addition & 2 deletions src/Laravel/Eloquent/PartialPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@

use ApiPlatform\State\Pagination\PartialPaginatorInterface;
use Illuminate\Pagination\AbstractPaginator;
use IteratorAggregate;

/**
* @implements IteratorAggregate<mixed,object>
* @implements \IteratorAggregate<mixed,object>
* @implements PartialPaginatorInterface<object>
*/
final class PartialPaginator implements PartialPaginatorInterface, \IteratorAggregate
Expand Down
11 changes: 8 additions & 3 deletions src/Laravel/Exception/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Exceptions\Handler as ExceptionsHandler;
use Negotiation\Negotiator;
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
Expand Down Expand Up @@ -54,6 +55,7 @@ public function __construct(
private readonly ?array $exceptionToStatus = null,
private readonly ?bool $debug = false,
private readonly ?array $errorFormats = null,
private readonly ?ExceptionHandler $decorated = null,
) {
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
$this->negotiator = $negotiator;
Expand All @@ -65,7 +67,7 @@ public function render($request, \Throwable $exception)
$apiOperation = $this->initializeOperation($request);

if (!$apiOperation) {
return parent::render($request, $exception);
return $this->decorated ? $this->decorated->render($request, $exception) : parent::render($request, $exception);
}

$formats = $this->errorFormats ?? ['jsonproblem' => ['application/problem+json']];
Expand Down Expand Up @@ -157,9 +159,12 @@ public function render($request, \Throwable $exception)
}

try {
return $this->apiPlatformController->__invoke($dup);
$response = $this->apiPlatformController->__invoke($dup);
$this->decorated->render($dup, $exception);

return $response;
} catch (\Throwable $e) {
return parent::render($dup, $e);
return $this->decorated ? $this->decorated->render($request, $exception) : parent::render($request, $exception);
}
}

Expand Down
Loading