@@ -32,19 +32,30 @@ class ErrorListener implements EventSubscriberInterface
32
32
protected $ controller ;
33
33
protected $ logger ;
34
34
protected $ debug ;
35
+ protected $ exceptionsMapping ;
35
36
36
- public function __construct ($ controller , LoggerInterface $ logger = null , bool $ debug = false )
37
+ public function __construct ($ controller , LoggerInterface $ logger = null , bool $ debug = false , array $ exceptionsMapping = [] )
37
38
{
38
39
$ this ->controller = $ controller ;
39
40
$ this ->logger = $ logger ;
40
41
$ this ->debug = $ debug ;
42
+ $ this ->exceptionsMapping = $ exceptionsMapping ;
41
43
}
42
44
43
45
public function logKernelException (ExceptionEvent $ event )
44
46
{
45
- $ e = FlattenException::createFromThrowable ($ event ->getThrowable ());
47
+ $ throwable = $ event ->getThrowable ();
48
+ $ logLevel = null ;
49
+ foreach ($ this ->exceptionsMapping as $ class => $ config ) {
50
+ if ($ throwable instanceof $ class && $ config ['log_level ' ]) {
51
+ $ logLevel = $ config ['log_level ' ];
52
+ break ;
53
+ }
54
+ }
55
+
56
+ $ e = FlattenException::createFromThrowable ($ throwable );
46
57
47
- $ this ->logException ($ event -> getThrowable () , sprintf ('Uncaught PHP Exception %s: "%s" at %s line %s ' , $ e ->getClass (), $ e ->getMessage (), $ e ->getFile (), $ e ->getLine ()));
58
+ $ this ->logException ($ throwable , sprintf ('Uncaught PHP Exception %s: "%s" at %s line %s ' , $ e ->getClass (), $ e ->getMessage (), $ e ->getFile (), $ e ->getLine ()), $ logLevel );
48
59
}
49
60
50
61
public function onKernelException (ExceptionEvent $ event )
@@ -53,8 +64,8 @@ public function onKernelException(ExceptionEvent $event)
53
64
return ;
54
65
}
55
66
56
- $ exception = $ event ->getThrowable ();
57
- $ request = $ this ->duplicateRequest ($ exception , $ event ->getRequest ());
67
+ $ throwable = $ event ->getThrowable ();
68
+ $ request = $ this ->duplicateRequest ($ throwable , $ event ->getRequest ());
58
69
59
70
try {
60
71
$ response = $ event ->getKernel ()->handle ($ request , HttpKernelInterface::SUB_REQUEST , false );
@@ -65,18 +76,25 @@ public function onKernelException(ExceptionEvent $event)
65
76
66
77
$ prev = $ e ;
67
78
do {
68
- if ($ exception === $ wrapper = $ prev ) {
79
+ if ($ throwable === $ wrapper = $ prev ) {
69
80
throw $ e ;
70
81
}
71
82
} while ($ prev = $ wrapper ->getPrevious ());
72
83
73
84
$ prev = new \ReflectionProperty ($ wrapper instanceof \Exception ? \Exception::class : \Error::class, 'previous ' );
74
85
$ prev ->setAccessible (true );
75
- $ prev ->setValue ($ wrapper , $ exception );
86
+ $ prev ->setValue ($ wrapper , $ throwable );
76
87
77
88
throw $ e ;
78
89
}
79
90
91
+ foreach ($ this ->exceptionsMapping as $ exception => $ config ) {
92
+ if ($ throwable instanceof $ exception && $ config ['status_code ' ]) {
93
+ $ response ->setStatusCode ($ config ['status_code ' ]);
94
+ break ;
95
+ }
96
+ }
97
+
80
98
$ event ->setResponse ($ response );
81
99
82
100
if ($ this ->debug ) {
@@ -124,10 +142,12 @@ public static function getSubscribedEvents(): array
124
142
/**
125
143
* Logs an exception.
126
144
*/
127
- protected function logException (\Throwable $ exception , string $ message ): void
145
+ protected function logException (\Throwable $ exception , string $ message, string $ logLevel = null ): void
128
146
{
129
147
if (null !== $ this ->logger ) {
130
- if (!$ exception instanceof HttpExceptionInterface || $ exception ->getStatusCode () >= 500 ) {
148
+ if (null !== $ logLevel ) {
149
+ $ this ->logger ->log ($ logLevel , $ message , ['exception ' => $ exception ]);
150
+ } elseif (!$ exception instanceof HttpExceptionInterface || $ exception ->getStatusCode () >= 500 ) {
131
151
$ this ->logger ->critical ($ message , ['exception ' => $ exception ]);
132
152
} else {
133
153
$ this ->logger ->error ($ message , ['exception ' => $ exception ]);
0 commit comments