Skip to content

Commit 8adf37f

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

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
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/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)