@@ -41,68 +41,68 @@ public static function render(/** @scrutinizer ignore-unused */ $request, Except
41
41
$ cfg = static ::getExceptionHandlerConfig ()['map ' ];
42
42
43
43
if ($ ex instanceof HttpException) {
44
- // Check if we have any exception configuration for this particular Http status code.
44
+ // Check if we have any exception configuration for this particular HTTP status code.
45
45
// This confing entry is guaranted to exist (at least 'default'). Enforced by tests.
46
46
$ http_code = $ ex ->getStatusCode ();
47
47
$ ex_cfg = $ cfg [ HttpException::class ][ $ http_code ] ?? null ;
48
48
$ ex_cfg = $ ex_cfg ?? $ cfg [ HttpException::class ]['default ' ];
49
- $ result = self ::processException ($ ex , $ ex_cfg , $ http_code );
49
+ $ result = self ::processException ($ ex , /** @scrutinizer ignore-type */ $ ex_cfg , $ http_code );
50
50
} elseif ($ ex instanceof ValidationException) {
51
51
// This entry is guaranted to exist. Enforced by tests.
52
52
$ http_code = HttpResponse::HTTP_UNPROCESSABLE_ENTITY ;
53
- $ ex_cfg = $ cfg [ HttpException::class ][ $ http_code ];
54
- $ result = self ::processException ($ ex , $ ex_cfg , $ http_code );
53
+ $ result = self ::processException ($ ex , $ cfg [ HttpException::class ][ $ http_code ], $ http_code );
55
54
}
56
55
57
56
if ($ result === null ) {
58
57
// This entry is guaranted to exist. Enforced by tests.
59
- $ http_code = HttpResponse::HTTP_INTERNAL_SERVER_ERROR ;
60
- $ ex_cfg = $ cfg ['default ' ];
61
- $ result = self ::processException ($ ex , $ ex_cfg , $ http_code );
58
+ $ result = self ::processException ($ ex , $ cfg ['default ' ], HttpResponse::HTTP_INTERNAL_SERVER_ERROR );
62
59
}
63
60
64
61
return $ result ;
65
62
}
66
63
67
- protected static function processException (\Exception $ ex , array $ ex_cfg , int $ http_code )
64
+ /**
65
+ * Handles given exception and produces valid HTTP response object.
66
+ *
67
+ * @param \Exception $ex Exception to be handled.
68
+ * @param array $ex_cfg ExceptionHandler's config excerpt related to $ex exception type.
69
+ * @param int $fallback_http_code HTTP code to be assigned to produced $ex related response in
70
+ * case configuration array lacks own `http_code` value.
71
+ *
72
+ * @return \Symfony\Component\HttpFoundation\Response
73
+ */
74
+ protected static function processException (\Exception $ ex , array $ ex_cfg , int $ fallback_http_code )
68
75
{
69
76
$ api_code = $ ex_cfg ['api_code ' ];
70
- $ http_code = $ ex_cfg ['http_code ' ] ?? $ http_code ;
77
+ $ http_code = $ ex_cfg ['http_code ' ] ?? $ fallback_http_code ;
71
78
$ msg_key = $ ex_cfg ['msg_key ' ] ?? null ;
72
79
$ msg_enforce = $ ex_cfg ['msg_enforce ' ] ?? false ;
73
80
74
81
// No message key, let's get exception message and if there's nothing useful, fallback to built-in one.
75
- $ error_message = $ ex ->getMessage ();
82
+ $ msg = $ ex ->getMessage ();
76
83
$ placeholders = [
77
84
'api_code ' => $ api_code ,
78
- 'message ' => ($ error_message !== '' ) ? $ error_message : '??? ' ,
85
+ 'message ' => ($ msg !== '' ) ? $ msg : '??? ' ,
79
86
];
80
87
81
88
// shall we enforce error message?
82
89
if ($ msg_enforce ) {
83
90
// yes, please.
84
91
if ($ msg_key === null ) {
85
92
// there's no msg_key configured for this exact code, so let's obtain our default message
86
- $ error_message = ($ msg_key === null ) ? static ::getErrorMessageForException ($ ex , $ http_code , $ placeholders ) : Lang::get ($ msg_key , $ placeholders );
93
+ $ msg = ($ msg_key === null ) ? static ::getErrorMessageForException ($ ex , $ http_code , $ placeholders )
94
+ : Lang::get ($ msg_key , $ placeholders );
87
95
}
88
96
} else {
89
- // nothing enforced, handling pipeline: ex_message -> user_defined msg -> http_ex -> default
90
- if ($ error_message === '' ) {
91
- $ error_message = ($ msg_key === null ) ? static ::getErrorMessageForException ($ ex , $ http_code , $ placeholders ) : Lang::get ($ msg_key , $ placeholders );
97
+ // nothing enforced, handling pipeline: ex_message -> user_defined_msg -> http_ex -> default
98
+ if ($ msg === '' ) {
99
+ $ msg = ($ msg_key === null ) ? static ::getErrorMessageForException ($ ex , $ http_code , $ placeholders )
100
+ : Lang::get ($ msg_key , $ placeholders );
92
101
}
93
102
}
94
103
95
104
// Lets' try to build the error response with what we have now
96
- $ result = static ::error ($ ex , $ api_code , $ http_code , $ error_message );
97
-
98
- if ($ result === null ) {
99
- $ ex_cfg = $ cfg [ HttpException::class ][ $ http_code ];
100
- $ api_code = $ ex_cfg ['api_code ' ] ?? BaseApiCodes::EX_VALIDATION_EXCEPTION ();
101
- $ http_code = $ ex_cfg ['http_code ' ] ?? $ http_code ;
102
- $ result = static ::error ($ ex , $ api_code , $ http_code , $ error_message );
103
- }
104
-
105
- return $ result ;
105
+ return static ::error ($ ex , $ api_code , $ http_code , $ msg );
106
106
}
107
107
108
108
/**
@@ -124,7 +124,8 @@ protected static function getErrorMessageForException(\Exception $ex, int $http_
124
124
} else {
125
125
// Still got nothing? Fall back to built-in generic message for this type of exception.
126
126
$ key = BaseApiCodes::getCodeMessageKey (($ ex instanceof HttpException)
127
- ? BaseApiCodes::EX_HTTP_EXCEPTION () : BaseApiCodes::NO_ERROR_MESSAGE ());
127
+ ? /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION ()
128
+ : /** @scrutinizer ignore-deprecated */ BaseApiCodes::NO_ERROR_MESSAGE ());
128
129
$ error_message = Lang::get ($ key , $ placeholders );
129
130
}
130
131
@@ -206,29 +207,33 @@ protected static function error(Exception $ex,
206
207
->build ();
207
208
}
208
209
209
- protected
210
- static function getExceptionHandlerDefaultConfig (): array
210
+ /**
211
+ * Returns default (built-in) exception handler config array.
212
+ *
213
+ * @return array
214
+ */
215
+ protected static function getExceptionHandlerDefaultConfig (): array
211
216
{
212
217
return [
213
218
'map ' => [
214
219
HttpException::class => [
215
220
// used by unauthenticated() to obtain api and http code for the exception
216
221
HttpResponse::HTTP_UNAUTHORIZED => [
217
- 'api_code ' => BaseApiCodes::EX_AUTHENTICATION_EXCEPTION (),
222
+ 'api_code ' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_AUTHENTICATION_EXCEPTION (),
218
223
],
219
224
// Required by ValidationException handler
220
225
HttpResponse::HTTP_UNPROCESSABLE_ENTITY => [
221
- 'api_code ' => BaseApiCodes::EX_VALIDATION_EXCEPTION (),
226
+ 'api_code ' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_VALIDATION_EXCEPTION (),
222
227
],
223
228
// default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set.
224
229
'default ' => [
225
- 'api_code ' => BaseApiCodes::EX_HTTP_EXCEPTION (),
230
+ 'api_code ' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION (),
226
231
'http_code ' => HttpResponse::HTTP_BAD_REQUEST ,
227
232
],
228
233
],
229
234
// default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set.
230
235
'default ' => [
231
- 'api_code ' => BaseApiCodes::EX_UNCAUGHT_EXCEPTION (),
236
+ 'api_code ' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_UNCAUGHT_EXCEPTION (),
232
237
'http_code ' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR ,
233
238
],
234
239
],
@@ -240,8 +245,7 @@ static function getExceptionHandlerDefaultConfig(): array
240
245
*
241
246
* @return array
242
247
*/
243
- protected
244
- static function getExceptionHandlerConfig (): array
248
+ protected static function getExceptionHandlerConfig (): array
245
249
{
246
250
return Util::mergeConfig (static ::getExceptionHandlerDefaultConfig (),
247
251
\Config::get (ResponseBuilder::CONF_KEY_EXCEPTION_HANDLER , []));
0 commit comments