Skip to content

Commit e7b3df1

Browse files
authored
fix(laravel): register error handler without graphql (api-platform#7168)
1 parent 383ac88 commit e7b3df1

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/Laravel/ApiPlatformDeferredProvider.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use ApiPlatform\GraphQl\Type\TypesContainerInterface;
2323
use ApiPlatform\JsonApi\Filter\SparseFieldset;
2424
use ApiPlatform\JsonApi\Filter\SparseFieldsetParameterProvider;
25+
use ApiPlatform\Laravel\Controller\ApiPlatformController;
2526
use ApiPlatform\Laravel\Eloquent\Extension\FilterQueryExtension;
2627
use ApiPlatform\Laravel\Eloquent\Extension\QueryExtensionInterface;
2728
use ApiPlatform\Laravel\Eloquent\Filter\BooleanFilter;
@@ -42,11 +43,13 @@
4243
use ApiPlatform\Laravel\Eloquent\State\LinksHandlerInterface;
4344
use ApiPlatform\Laravel\Eloquent\State\PersistProcessor;
4445
use ApiPlatform\Laravel\Eloquent\State\RemoveProcessor;
46+
use ApiPlatform\Laravel\Exception\ErrorHandler;
4547
use ApiPlatform\Laravel\Metadata\CacheResourceCollectionMetadataFactory;
4648
use ApiPlatform\Laravel\Metadata\ParameterValidationResourceMetadataCollectionFactory;
4749
use ApiPlatform\Laravel\State\ParameterValidatorProvider;
4850
use ApiPlatform\Laravel\State\SwaggerUiProcessor;
4951
use ApiPlatform\Laravel\State\ValidateProvider;
52+
use ApiPlatform\Metadata\IdentifiersExtractorInterface;
5053
use ApiPlatform\Metadata\InflectorInterface;
5154
use ApiPlatform\Metadata\Operation\PathSegmentNameGeneratorInterface;
5255
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
@@ -80,9 +83,11 @@
8083
use ApiPlatform\State\Provider\ParameterProvider;
8184
use ApiPlatform\State\Provider\SecurityParameterProvider;
8285
use ApiPlatform\State\ProviderInterface;
86+
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerInterface;
8387
use Illuminate\Contracts\Foundation\Application;
8488
use Illuminate\Contracts\Support\DeferrableProvider;
8589
use Illuminate\Support\ServiceProvider;
90+
use Negotiation\Negotiator;
8691
use Psr\Log\LoggerInterface;
8792
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
8893
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
@@ -289,6 +294,26 @@ private function registerGraphQl(): void
289294
$app->make(InflectorInterface::class)
290295
);
291296
});
297+
298+
$this->app->singleton(
299+
ExceptionHandlerInterface::class,
300+
function (Application $app) {
301+
/** @var ConfigRepository */
302+
$config = $app['config'];
303+
304+
return new ErrorHandler(
305+
$app,
306+
$app->make(ResourceMetadataCollectionFactoryInterface::class),
307+
$app->make(ApiPlatformController::class),
308+
$app->make(IdentifiersExtractorInterface::class),
309+
$app->make(ResourceClassResolverInterface::class),
310+
$app->make(Negotiator::class),
311+
$config->get('api-platform.exception_to_status'),
312+
$config->get('app.debug'),
313+
$config->get('api-platform.error_formats')
314+
);
315+
}
316+
);
292317
}
293318

294319
/**
@@ -329,6 +354,7 @@ public function provides(): array
329354
ResourceMetadataCollectionFactoryInterface::class,
330355
'api_platform.graphql.state_provider.parameter',
331356
FieldsBuilderEnumInterface::class,
357+
ExceptionHandlerInterface::class,
332358
];
333359
}
334360
}

src/Laravel/ApiPlatformProvider.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
7777
use ApiPlatform\Laravel\ApiResource\Error;
7878
use ApiPlatform\Laravel\ApiResource\ValidationError;
79-
use ApiPlatform\Laravel\Controller\ApiPlatformController;
8079
use ApiPlatform\Laravel\Controller\DocumentationController;
8180
use ApiPlatform\Laravel\Controller\EntrypointController;
8281
use ApiPlatform\Laravel\Eloquent\Filter\JsonApi\SortFilterParameterProvider;
@@ -88,7 +87,6 @@
8887
use ApiPlatform\Laravel\Eloquent\Metadata\ResourceClassResolver as EloquentResourceClassResolver;
8988
use ApiPlatform\Laravel\Eloquent\PropertyAccess\PropertyAccessor as EloquentPropertyAccessor;
9089
use ApiPlatform\Laravel\Eloquent\Serializer\SerializerContextBuilder as EloquentSerializerContextBuilder;
91-
use ApiPlatform\Laravel\Exception\ErrorHandler;
9290
use ApiPlatform\Laravel\GraphQl\Controller\EntrypointController as GraphQlEntrypointController;
9391
use ApiPlatform\Laravel\GraphQl\Controller\GraphiQlController;
9492
use ApiPlatform\Laravel\JsonApi\State\JsonApiProvider;
@@ -156,7 +154,6 @@
156154
use ApiPlatform\State\ProviderInterface;
157155
use ApiPlatform\State\SerializerContextBuilderInterface;
158156
use Illuminate\Config\Repository as ConfigRepository;
159-
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerInterface;
160157
use Illuminate\Contracts\Foundation\Application;
161158
use Illuminate\Routing\Router;
162159
use Illuminate\Support\ServiceProvider;
@@ -927,26 +924,6 @@ public function register(): void
927924
return new Inflector();
928925
});
929926

930-
$this->app->singleton(
931-
ExceptionHandlerInterface::class,
932-
function (Application $app) {
933-
/** @var ConfigRepository */
934-
$config = $app['config'];
935-
936-
return new ErrorHandler(
937-
$app,
938-
$app->make(ResourceMetadataCollectionFactoryInterface::class),
939-
$app->make(ApiPlatformController::class),
940-
$app->make(IdentifiersExtractorInterface::class),
941-
$app->make(ResourceClassResolverInterface::class),
942-
$app->make(Negotiator::class),
943-
$config->get('api-platform.exception_to_status'),
944-
$config->get('app.debug'),
945-
$config->get('api-platform.error_formats')
946-
);
947-
}
948-
);
949-
950927
if ($this->app->runningInConsole()) {
951928
$this->commands([
952929
Console\InstallCommand::class,

0 commit comments

Comments
 (0)