Skip to content
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
4 changes: 4 additions & 0 deletions src/Laravel/ApiResource/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ public static function createFromException(\Exception|\Throwable $exception, int
{
$headers = ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) ? $exception->getHeaders() : [];

if ($exception instanceof ProblemExceptionInterface) {
return new self($exception->getTitle(), $exception->getDetail(), $exception->getStatus(), $exception->getTrace(), $exception->getInstance(), $exception->getType(), $headers);
}

return new self('An error occurred', $exception->getMessage(), $status, $exception->getTrace(), type: '/errors/'.$status, headers: $headers);
}

Expand Down
14 changes: 14 additions & 0 deletions src/Laravel/Tests/JsonProblemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ public function testRetrieveErrorHtml(): void
</html>', $response->getContent());
}

public function testProblemExceptionInterface(): void
{
$response = $this->get('/api/teapot', headers: ['accept' => 'application/json']);
$response->assertStatus(418);
$response->assertHeader('content-type', 'application/problem+json; charset=utf-8');
$response->assertJsonFragment([
'type' => '/problem/teapot',
'title' => 'I\'m a teapot',
'status' => 418,
'detail' => 'No coffee here',
'instance' => '/teapot',
]);
}

/**
* @return list<array{0: string, 1: string, 2: array<string, mixed>}>
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Laravel/workbench/app/ApiResource/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use ApiPlatform\Metadata\Get;
use Workbench\App\State\CustomProvider;
use Workbench\App\State\CustomProviderWithDependency;
use Workbench\App\State\TeapotProvider;

#[Get(uriTemplate: 'teapot', name: 'teapot', provider: TeapotProvider::class)]
#[Get(uriTemplate: 'custom_service_provider', provider: CustomProvider::class)]
#[Get(uriTemplate: 'custom_service_provider_with_dependency', provider: CustomProviderWithDependency::class)]
class ServiceProvider
Expand Down
55 changes: 55 additions & 0 deletions src/Laravel/workbench/app/Exceptions/TeapotException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Workbench\App\Exceptions;

use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class TeapotException extends \Exception implements ProblemExceptionInterface, HttpExceptionInterface
{
public function getStatusCode(): int
{
return 418;
}

public function getHeaders(): array
{
return [];
}

public function getType(): string
{
return '/problem/teapot';
}

public function getTitle(): ?string
{
return 'I\'m a teapot';
}

public function getStatus(): ?int
{
return 418;
}

public function getDetail(): ?string
{
return 'No coffee here';
}

public function getInstance(): ?string
{
return '/teapot';
}
}
30 changes: 30 additions & 0 deletions src/Laravel/workbench/app/State/TeapotProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Workbench\App\State;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use Workbench\App\ApiResource\ServiceProvider;
use Workbench\App\Exceptions\TeapotException;

/**
* @implements ProviderInterface<ServiceProvider>
*/
class TeapotProvider implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
throw new TeapotException('I\'m boiling');
}
}
4 changes: 4 additions & 0 deletions src/State/ApiResource/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ public static function createFromException(\Exception|\Throwable $exception, int
{
$headers = ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) ? $exception->getHeaders() : [];

if ($exception instanceof ProblemExceptionInterface) {
return new self($exception->getTitle(), $exception->getDetail(), $exception->getStatus(), $exception->getTrace(), $exception->getInstance(), $exception->getType(), $headers);
}

return new self('An error occurred', $exception->getMessage(), $status, $exception->getTrace(), type: "/errors/$status", headers: $headers, previous: $exception->getPrevious());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Parameters/StrictParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function getResources(): array
public function testErrorAsParameterIsNotAllowed(): void
{
self::createClient()->request('GET', 'strict_query_parameters?bar=test');
$this->assertJsonContains(['detail' => 'Parameter not supported']);
$this->assertJsonContains(['detail' => 'Parameter "bar" not supported']);
$this->assertResponseStatusCodeSame(400);
}

Expand Down
Loading