|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the bugloos/error-response-bundle project. |
|
5
|
|
|
* (c) Bugloos <https://bugloos.com/> |
|
6
|
|
|
* For the full copyright and license information, please view |
|
7
|
|
|
* the LICENSE file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Bugloos\ErrorResponseBundle\Normalizer; |
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
13
|
|
|
use Symfony\Component\ErrorHandler\Exception\FlattenException; |
|
14
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractErrorNormalizer implements NormalizerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
public const TITLE = 'title'; |
|
19
|
|
|
public const STATUS = 'statusCode'; |
|
20
|
|
|
public const MESSAGE = 'message'; |
|
21
|
|
|
public const ERRORS = 'errors'; |
|
22
|
|
|
|
|
23
|
|
|
protected $debug; |
|
24
|
|
|
protected $defaultContext = [ |
|
25
|
|
|
self::TITLE => 'An error occurred', |
|
26
|
|
|
self::ERRORS => [], |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(ParameterBagInterface $bag, array $defaultContext = []) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->debug = $bag->get('kernel.debug'); |
|
32
|
|
|
$this->defaultContext = $defaultContext + $this->defaultContext; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function createResponse(bool $debug, FlattenException $object, array $context = []): array |
|
36
|
|
|
{ |
|
37
|
|
|
$data = [ |
|
38
|
|
|
self::TITLE => $context['title'], |
|
39
|
|
|
self::STATUS => $context['status'] ?? $object->getStatusCode(), |
|
40
|
|
|
self::MESSAGE => $debug ? $object->getMessage() : $object->getStatusText(), |
|
41
|
|
|
self::ERRORS => $context['errors'], |
|
42
|
|
|
]; |
|
43
|
|
|
if ($debug) { |
|
44
|
|
|
$data['class'] = $object->getClass(); |
|
45
|
|
|
$data['trace'] = $object->getTrace(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $data; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|