Skip to content

Commit e19de04

Browse files
committed
Modified implementation since monolog excluded_http_codes depends on the HttpException class, not interface
1 parent d67cf17 commit e19de04

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

src/Exception/ApiProblemHttpException.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,32 @@
44

55
namespace Phpro\ApiProblemBundle\Exception;
66

7-
use Phpro\ApiProblem\Exception\ApiProblemException;
7+
use Phpro\ApiProblem\ApiProblemInterface;
88
use Symfony\Component\HttpFoundation\Response;
9-
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
9+
use Symfony\Component\HttpKernel\Exception\HttpException;
1010

11-
class ApiProblemHttpException extends ApiProblemException implements HttpExceptionInterface
11+
class ApiProblemHttpException extends HttpException
1212
{
13+
private $apiProblem;
14+
15+
public function __construct(ApiProblemInterface $apiProblem)
16+
{
17+
$data = $apiProblem->toArray();
18+
$message = $data['detail'] ?? ($data['title'] ?? '');
19+
$code = (int) ($data['status'] ?? 0);
20+
21+
parent::__construct($code, $message);
22+
$this->apiProblem = $apiProblem;
23+
}
24+
25+
public function getApiProblem(): ApiProblemInterface
26+
{
27+
return $this->apiProblem;
28+
}
29+
1330
public function getStatusCode()
1431
{
15-
return $this->code > 0 ? $this->code : Response::HTTP_BAD_REQUEST;
32+
return parent::getStatusCode() > 0 ? parent::getStatusCode() : Response::HTTP_BAD_REQUEST;
1633
}
1734

1835
public function getHeaders()

src/Transformer/ApiProblemExceptionTransformer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
use Phpro\ApiProblem\ApiProblemInterface;
88
use Phpro\ApiProblem\Exception\ApiProblemException;
9+
use Phpro\ApiProblemBundle\Exception\ApiProblemHttpException;
910

1011
class ApiProblemExceptionTransformer implements ExceptionTransformerInterface
1112
{
1213
/**
13-
* @param ApiProblemException $exception
14+
* @param ApiProblemException|ApiProblemHttpException $exception
1415
*/
1516
public function transform(\Throwable $exception): ApiProblemInterface
1617
{
@@ -19,6 +20,6 @@ public function transform(\Throwable $exception): ApiProblemInterface
1920

2021
public function accepts(\Throwable $exception): bool
2122
{
22-
return $exception instanceof ApiProblemException;
23+
return $exception instanceof ApiProblemException || $exception instanceof ApiProblemHttpException;
2324
}
2425
}

test/Exception/ApiProblemHttpExceptionTest.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace PhproTest\ApiProblemBundle\Exception;
66

7-
use Phpro\ApiProblem\Exception\ApiProblemException;
87
use Phpro\ApiProblem\Http\HttpApiProblem;
98
use Phpro\ApiProblemBundle\Exception\ApiProblemHttpException;
9+
use Phpro\ApiProblemBundle\Transformer\ApiProblemExceptionTransformer;
1010
use PHPUnit\Framework\TestCase;
1111
use Prophecy\Prophecy\ObjectProphecy;
12-
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
12+
use Symfony\Component\HttpKernel\Exception\HttpException;
1313

1414
class ApiProblemHttpExceptionTest extends TestCase
1515
{
@@ -25,19 +25,28 @@ protected function setUp(): void/* The :void return type declaration that should
2525
}
2626

2727
/** @test */
28-
public function it_is_an_instance_of_ApiProblemException(): void
28+
public function it_is_accepted_by_the_ApiProblemExceptionTransformer(): void
2929
{
30-
$exception = new ApiProblemHttpException($this->apiProblem->reveal());
30+
$transformer = new ApiProblemExceptionTransformer();
3131

32-
$this->assertInstanceOf(ApiProblemException::class, $exception);
32+
$this->assertTrue($transformer->accepts(new ApiProblemHttpException($this->apiProblem->reveal())));
3333
}
3434

3535
/** @test */
3636
public function it_is_an_instance_of_HttpException(): void
3737
{
3838
$exception = new ApiProblemHttpException($this->apiProblem->reveal());
3939

40-
$this->assertInstanceOf(HttpExceptionInterface::class, $exception);
40+
$this->assertInstanceOf(HttpException::class, $exception);
41+
}
42+
43+
/** @test */
44+
public function it_contains_an_api_problem(): void
45+
{
46+
$apiProblem = $this->apiProblem->reveal();
47+
48+
$exception = new ApiProblemHttpException($apiProblem);
49+
$this->assertEquals($apiProblem, $exception->getApiProblem());
4150
}
4251

4352
/** @test */

0 commit comments

Comments
 (0)