Skip to content

Commit 9d11ff3

Browse files
committed
fix(laravel): decorate error handler
1 parent 221e5d9 commit 9d11ff3

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

src/Laravel/ApiPlatformDeferredProvider.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
use ApiPlatform\State\Provider\ParameterProvider;
8484
use ApiPlatform\State\Provider\SecurityParameterProvider;
8585
use ApiPlatform\State\ProviderInterface;
86-
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerInterface;
86+
use Illuminate\Contracts\Debug\ExceptionHandler;
8787
use Illuminate\Contracts\Foundation\Application;
8888
use Illuminate\Contracts\Support\DeferrableProvider;
8989
use Illuminate\Support\ServiceProvider;
@@ -252,9 +252,9 @@ public function register(): void
252252
);
253253
});
254254

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

@@ -267,7 +267,8 @@ function (Application $app) {
267267
$app->make(Negotiator::class),
268268
$config->get('api-platform.exception_to_status'),
269269
$config->get('app.debug'),
270-
$config->get('api-platform.error_formats')
270+
$config->get('api-platform.error_formats'),
271+
$decorated
271272
);
272273
}
273274
);

src/Laravel/Eloquent/Paginator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
use ApiPlatform\State\Pagination\HasNextPagePaginatorInterface;
1717
use ApiPlatform\State\Pagination\PaginatorInterface;
1818
use Illuminate\Pagination\LengthAwarePaginator;
19-
use IteratorAggregate;
2019

2120
/**
22-
* @implements IteratorAggregate<mixed,object>
21+
* @implements \IteratorAggregate<mixed,object>
2322
* @implements PaginatorInterface<object>
2423
*/
2524
final class Paginator implements PaginatorInterface, HasNextPagePaginatorInterface, \IteratorAggregate

src/Laravel/Eloquent/PartialPaginator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515

1616
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
1717
use Illuminate\Pagination\AbstractPaginator;
18-
use IteratorAggregate;
1918

2019
/**
21-
* @implements IteratorAggregate<mixed,object>
20+
* @implements \IteratorAggregate<mixed,object>
2221
* @implements PartialPaginatorInterface<object>
2322
*/
2423
final class PartialPaginator implements PartialPaginatorInterface, \IteratorAggregate

src/Laravel/Exception/ErrorHandler.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Illuminate\Auth\Access\AuthorizationException;
2828
use Illuminate\Auth\AuthenticationException;
2929
use Illuminate\Contracts\Container\Container;
30+
use Illuminate\Contracts\Debug\ExceptionHandler;
3031
use Illuminate\Foundation\Exceptions\Handler as ExceptionsHandler;
3132
use Negotiation\Negotiator;
3233
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
@@ -54,6 +55,7 @@ public function __construct(
5455
private readonly ?array $exceptionToStatus = null,
5556
private readonly ?bool $debug = false,
5657
private readonly ?array $errorFormats = null,
58+
private readonly ?ExceptionHandler $decorated = null,
5759
) {
5860
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
5961
$this->negotiator = $negotiator;
@@ -65,7 +67,7 @@ public function render($request, \Throwable $exception)
6567
$apiOperation = $this->initializeOperation($request);
6668

6769
if (!$apiOperation) {
68-
return parent::render($request, $exception);
70+
return $this->decorated ? $this->decorated->render($request, $exception) : parent::render($request, $exception);
6971
}
7072

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

159161
try {
160-
return $this->apiPlatformController->__invoke($dup);
162+
$response = $this->apiPlatformController->__invoke($dup);
163+
$this->decorated->render($dup, $exception);
164+
165+
return $response;
161166
} catch (\Throwable $e) {
162-
return parent::render($dup, $e);
167+
return $this->decorated ? $this->decorated->render($request, $exception) : parent::render($request, $exception);
163168
}
164169
}
165170

0 commit comments

Comments
 (0)