Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
ApiErrorRenderer | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
createApiResponse | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace Apie\Common\ErrorHandler; |
4 | |
5 | use Apie\Common\ContextBuilders\Exceptions\WrongTokenException; |
6 | use Apie\Common\Events\AddAuthenticationCookie; |
7 | use Apie\Core\Exceptions\HttpStatusCodeException; |
8 | use Symfony\Component\HttpFoundation\JsonResponse; |
9 | use Symfony\Component\HttpFoundation\Response; |
10 | use Throwable; |
11 | |
12 | class ApiErrorRenderer |
13 | { |
14 | /** |
15 | * @TODO move to PSR request + response? |
16 | */ |
17 | public function createApiResponse(Throwable $error): Response |
18 | { |
19 | $statusCode = $error instanceof HttpStatusCodeException ? $error->getStatusCode() : 500; |
20 | $response = new JsonResponse( |
21 | [ |
22 | 'message' => $error->getMessage(), |
23 | 'code' => $error->getCode(), |
24 | ], |
25 | $statusCode |
26 | ); |
27 | if ($error instanceof WrongTokenException) { |
28 | $response->headers->clearCookie(AddAuthenticationCookie::COOKIE_NAME); |
29 | } |
30 | return $response; |
31 | } |
32 | } |