File tree Expand file tree Collapse file tree 3 files changed +127
-0
lines changed Expand file tree Collapse file tree 3 files changed +127
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+
4
+ namespace HalloVerden \HttpExceptionsBundle \Services ;
5
+
6
+
7
+ use HalloVerden \HttpExceptions \Http \HttpException ;
8
+ use HalloVerden \HttpExceptions \InternalServerErrorException ;
9
+ use HalloVerden \HttpExceptionsBundle \Interfaces \Services \ExceptionConverterServiceInterface ;
10
+ use Symfony \Component \HttpKernel \Exception \HttpExceptionInterface ;
11
+
12
+ class ExceptionConverterService implements ExceptionConverterServiceInterface {
13
+ const ERROR_UNKNOWN = 'UNKNOWN_ERROR ' ;
14
+
15
+ /**
16
+ * @param \Throwable $throwable
17
+ *
18
+ * @return HttpExceptionInterface
19
+ */
20
+ public function createHttpExceptionFromThrowable (\Throwable $ throwable ): HttpExceptionInterface {
21
+ if ($ throwable instanceof HttpException) {
22
+ return $ throwable ;
23
+ }
24
+
25
+ return new InternalServerErrorException (self ::ERROR_UNKNOWN , $ throwable );
26
+ }
27
+
28
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+
4
+ namespace HalloVerden \HttpExceptionsBundle \Services ;
5
+
6
+
7
+ use HalloVerden \HttpExceptionsBundle \Helpers \FlattenExceptionHelper ;
8
+ use HalloVerden \HttpExceptionsBundle \Interfaces \Services \ExceptionLogServiceInterface ;
9
+ use Psr \Log \LoggerAwareTrait ;
10
+ use Psr \Log \LoggerInterface ;
11
+ use Symfony \Component \HttpKernel \Exception \HttpExceptionInterface ;
12
+
13
+ class ExceptionLogService implements ExceptionLogServiceInterface {
14
+ use LoggerAwareTrait;
15
+
16
+ /**
17
+ * @param HttpExceptionInterface $exception
18
+ */
19
+ public function logHttpException (HttpExceptionInterface $ exception ): void {
20
+ if (!$ this ->logger ) {
21
+ return ;
22
+ }
23
+
24
+ $ context = FlattenExceptionHelper::createFromThrowable ($ exception )->toArray ();
25
+ if ($ exception ->getStatusCode () >= 500 ) {
26
+ $ this ->logger ->error ("InternalServerError " , $ context );
27
+ } else {
28
+ $ this ->logger ->info ("Exception was thrown " , $ context );
29
+ }
30
+ }
31
+
32
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+
4
+ namespace HalloVerden \HttpExceptionsBundle \Services ;
5
+
6
+
7
+ use HalloVerden \HttpExceptions \DataExceptionInterface ;
8
+ use HalloVerden \HttpExceptionsBundle \Helpers \FlattenExceptionHelper ;
9
+ use HalloVerden \HttpExceptionsBundle \Interfaces \Services \ExceptionResponseServiceInterface ;
10
+ use Symfony \Component \HttpFoundation \JsonResponse ;
11
+ use Symfony \Component \HttpFoundation \Response ;
12
+ use Symfony \Component \HttpKernel \Exception \HttpExceptionInterface ;
13
+
14
+ class ExceptionResponseService implements ExceptionResponseServiceInterface {
15
+
16
+ /**
17
+ * @var bool
18
+ */
19
+ private $ debug ;
20
+
21
+ /**
22
+ * ExceptionResponseService constructor.
23
+ *
24
+ * @param bool $debug
25
+ */
26
+ public function __construct (bool $ debug = false ) {
27
+ $ this ->debug = $ debug ;
28
+ }
29
+
30
+ /**
31
+ * @param HttpExceptionInterface $exception
32
+ *
33
+ * @return Response
34
+ */
35
+ public function createResponseFromHttpException (HttpExceptionInterface $ exception ): Response {
36
+ // Note: Encoding options needs to be set before data
37
+ return JsonResponse::create (null , $ exception ->getStatusCode (), $ exception ->getHeaders ())
38
+ ->setEncodingOptions (JsonResponse::DEFAULT_ENCODING_OPTIONS | JSON_INVALID_UTF8_IGNORE )
39
+ ->setData ($ this ->getData ($ exception ));
40
+ }
41
+
42
+ /**
43
+ * @param HttpExceptionInterface $exception
44
+ *
45
+ * @return array
46
+ */
47
+ protected function getData (HttpExceptionInterface $ exception ): array {
48
+ $ data = null ;
49
+
50
+ if ($ exception instanceof DataExceptionInterface) {
51
+ $ data = $ exception ->getData ();
52
+ }
53
+
54
+ if (!$ data ) {
55
+ $ data = [
56
+ 'error ' => $ exception ->getMessage ()
57
+ ];
58
+ }
59
+
60
+ if ($ this ->debug ) {
61
+ $ data ['debug ' ] = FlattenExceptionHelper::createFromThrowable ($ exception )->toArray ();
62
+ }
63
+
64
+ return $ data ;
65
+ }
66
+
67
+ }
You can’t perform that action at this time.
0 commit comments