Skip to content

Commit d67cf17

Browse files
committed
Added Exception class implementing HttpExceptionInterface
1 parent a0fef11 commit d67cf17

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Api Problem Bundle
77

88
This package provides a [RFC7807](https://tools.ietf.org/html/rfc7807) Problem details exception listener for Symfony.
9-
Internal, this package uses the models provided by `phpro/api-problem`](https://www.github.com/phpro/api-problem).
9+
Internal, this package uses the models provided by [`phpro/api-problem`](https://www.github.com/phpro/api-problem).
1010
When an `ApiProblemException` is triggered, this bundle will return the correct response.
1111

1212

@@ -69,6 +69,9 @@ Body:
6969
}
7070
```
7171

72+
As an alternative, use ```ApiProblemHttpException``` instead of ```ApiProblemException```, to make it possible to
73+
[exclude the specific status code from the log](https://symfony.com/doc/current/logging/monolog_exclude_http_codes.html)
74+
7275
## Adding custom exception transformations
7376

7477
Currently, we automatically transform exceptions from following packages to an ApiProblem instance:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phpro\ApiProblemBundle\Exception;
6+
7+
use Phpro\ApiProblem\Exception\ApiProblemException;
8+
use Symfony\Component\HttpFoundation\Response;
9+
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
10+
11+
class ApiProblemHttpException extends ApiProblemException implements HttpExceptionInterface
12+
{
13+
public function getStatusCode()
14+
{
15+
return $this->code > 0 ? $this->code : Response::HTTP_BAD_REQUEST;
16+
}
17+
18+
public function getHeaders()
19+
{
20+
return ['Content-Type' => 'application/problem+json'];
21+
}
22+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhproTest\ApiProblemBundle\Exception;
6+
7+
use Phpro\ApiProblem\Exception\ApiProblemException;
8+
use Phpro\ApiProblem\Http\HttpApiProblem;
9+
use Phpro\ApiProblemBundle\Exception\ApiProblemHttpException;
10+
use PHPUnit\Framework\TestCase;
11+
use Prophecy\Prophecy\ObjectProphecy;
12+
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
13+
14+
class ApiProblemHttpExceptionTest extends TestCase
15+
{
16+
/**
17+
* @var HttpApiProblem|ObjectProphecy
18+
*/
19+
private $apiProblem;
20+
21+
protected function setUp(): void/* The :void return type declaration that should be here would cause a BC issue */
22+
{
23+
$this->apiProblem = $this->prophesize(HttpApiProblem::class);
24+
$this->apiProblem->toArray()->willReturn([]);
25+
}
26+
27+
/** @test */
28+
public function it_is_an_instance_of_ApiProblemException(): void
29+
{
30+
$exception = new ApiProblemHttpException($this->apiProblem->reveal());
31+
32+
$this->assertInstanceOf(ApiProblemException::class, $exception);
33+
}
34+
35+
/** @test */
36+
public function it_is_an_instance_of_HttpException(): void
37+
{
38+
$exception = new ApiProblemHttpException($this->apiProblem->reveal());
39+
40+
$this->assertInstanceOf(HttpExceptionInterface::class, $exception);
41+
}
42+
43+
/** @test */
44+
public function it_returns_the_correct_http_headers(): void
45+
{
46+
$exception = new ApiProblemHttpException($this->apiProblem->reveal());
47+
48+
$this->assertEquals(['Content-Type' => 'application/problem+json'], $exception->getHeaders());
49+
}
50+
51+
/** @test */
52+
public function it_returns_the_correct_default_http_statuscode(): void
53+
{
54+
$exception = new ApiProblemHttpException($this->apiProblem->reveal());
55+
56+
$this->assertEquals(400, $exception->getStatusCode());
57+
}
58+
59+
/** @test */
60+
public function it_returns_the_correct_specified_http_statuscode(): void
61+
{
62+
$this->apiProblem->toArray()->willReturn(['status' => 401]);
63+
$exception = new ApiProblemHttpException($this->apiProblem->reveal());
64+
65+
$this->assertEquals(401, $exception->getStatusCode());
66+
}
67+
}

0 commit comments

Comments
 (0)