| Total Complexity | 59 |
| Total Lines | 539 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Handler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class Handler implements ExceptionHandlerContract |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * The container implementation. |
||
| 54 | * |
||
| 55 | * @var \Illuminate\Contracts\Container\Container |
||
| 56 | */ |
||
| 57 | protected $container; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * A list of the exception types that are not reported. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $dont_report = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * A list of the internal exception types that should not be reported. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $internal_dont_report = [ |
||
| 72 | AuthenticationException::class, |
||
| 73 | AuthorizationException::class, |
||
| 74 | HttpException::class, |
||
| 75 | HttpResponseException::class, |
||
| 76 | ModelNotFoundException::class, |
||
| 77 | SuspiciousOperationException::class, |
||
| 78 | TokenMismatchException::class, |
||
| 79 | ValidationException::class, |
||
| 80 | ]; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * A list of the inputs that are never flashed for validation exceptions. |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $dont_flash = [ |
||
| 88 | 'password', |
||
| 89 | 'password_confirmation', |
||
| 90 | ]; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Create a new exception handler instance. |
||
| 94 | * |
||
| 95 | * @param \Illuminate\Contracts\Container\Container $container The app container. |
||
| 96 | * |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | public function __construct(Container $container) |
||
| 100 | { |
||
| 101 | $this->container = $container; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Report or log an exception. |
||
| 106 | * |
||
| 107 | * @param \Throwable $e The throwable exception. |
||
| 108 | * |
||
| 109 | * @throws \Exception Throw the exception. |
||
| 110 | * |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | public function report(Throwable $e) |
||
| 114 | { |
||
| 115 | if ($this->shouldnt_report($e)) { |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | $report_callable = [$e, 'report']; |
||
| 120 | |||
| 121 | if (is_callable($report_callable)) { |
||
| 122 | $this->container->call($report_callable); |
||
| 123 | |||
| 124 | return; |
||
| 125 | } |
||
| 126 | |||
| 127 | try { |
||
| 128 | $logger = $this->container->make(LoggerInterface::class); |
||
| 129 | } catch (Exception $ex) { |
||
| 130 | throw $e; |
||
| 131 | } |
||
| 132 | |||
| 133 | $logger->error( |
||
| 134 | $e->getMessage(), |
||
| 135 | array_merge( |
||
| 136 | $this->exception_context($e), |
||
| 137 | $this->context(), |
||
| 138 | ['exception' => $e] |
||
| 139 | ) |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Determine if the exception should be reported. |
||
| 145 | * |
||
| 146 | * @param \Throwable $e The throwable exception. |
||
| 147 | * |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function should_report(Throwable $e) |
||
| 151 | { |
||
| 152 | return !$this->shouldnt_report($e); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Determine if the exception is in the "do not report" list. |
||
| 157 | * |
||
| 158 | * @param \Throwable $e The throwable exception. |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | protected function shouldnt_report(Throwable $e) |
||
| 163 | { |
||
| 164 | $dont_report = array_merge($this->dont_report, $this->internal_dont_report); |
||
| 165 | |||
| 166 | return !is_null( |
||
| 167 | Arr::first( |
||
| 168 | $dont_report, |
||
| 169 | function ($type) use ($e) { |
||
| 170 | return $e instanceof $type; |
||
| 171 | } |
||
| 172 | ) |
||
| 173 | ); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get the default exception context variables for logging. |
||
| 178 | * |
||
| 179 | * @param \Throwable $e The throwable exception. |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | protected function exception_context(Throwable $e) |
||
| 184 | { |
||
| 185 | return []; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the default context variables for logging. |
||
| 190 | * |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | protected function context() |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Render an exception into an HTTP response. |
||
| 209 | * |
||
| 210 | * @param \Illuminate\Http\Request $request The app request. |
||
| 211 | * @param \Throwable $e The throwable exception. |
||
| 212 | * |
||
| 213 | * @throws \Throwable Throw the exception. |
||
| 214 | * |
||
| 215 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 216 | */ |
||
| 217 | public function render($request, Throwable $e) |
||
| 218 | { |
||
| 219 | $response = $e->render($request); |
||
| 220 | if (method_exists($e, 'render') && $response) { |
||
| 221 | return Router::toResponse($request, $response); |
||
| 222 | } elseif ($e instanceof Responsable) { |
||
| 223 | return $e->toResponse($request); |
||
| 224 | } |
||
| 225 | |||
| 226 | $e = $this->prepare_exception($e); |
||
| 227 | |||
| 228 | if ($e instanceof HttpResponseException) { |
||
| 229 | return $e->getResponse(); |
||
| 230 | } elseif ($e instanceof AuthenticationException) { |
||
| 231 | return $this->unauthenticated($request, $e); |
||
| 232 | } elseif ($e instanceof ValidationException) { |
||
| 233 | return $this->convert_validation_exception_to_response($e, $request); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $request->expectsJson() |
||
| 237 | ? $this->prepare_json_response($request, $e) |
||
| 238 | : $this->prepare_response($request, $e); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Prepare exception for rendering. |
||
| 243 | * |
||
| 244 | * @param \Throwable $e The throwable exception. |
||
| 245 | * |
||
| 246 | * @return \Throwable |
||
| 247 | */ |
||
| 248 | protected function prepare_exception(Throwable $e) |
||
| 249 | { |
||
| 250 | if ($e instanceof ModelNotFoundException) { |
||
| 251 | $e = new NotFoundHttpException($e->getMessage(), $e); |
||
| 252 | } elseif ($e instanceof AuthorizationException) { |
||
| 253 | $e = new AccessDeniedHttpException($e->getMessage(), $e); |
||
| 254 | } elseif ($e instanceof TokenMismatchException) { |
||
| 255 | $e = new HttpException(419, $e->getMessage(), $e); |
||
| 256 | } elseif ($e instanceof SuspiciousOperationException) { |
||
| 257 | $e = new NotFoundHttpException('Bad hostname provided.', $e); |
||
| 258 | } |
||
| 259 | |||
| 260 | return $e; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Convert an authentication exception into a response. |
||
| 265 | * |
||
| 266 | * @param \Illuminate\Http\Request $request The app request. |
||
| 267 | * @param \Illuminate\Auth\AuthenticationException $exception The authenticated exception. |
||
| 268 | * |
||
| 269 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 270 | */ |
||
| 271 | protected function unauthenticated($request, AuthenticationException $exception) |
||
| 272 | { |
||
| 273 | return $request->expectsJson() |
||
| 274 | ? response()->json(['message' => $exception->getMessage()], 401) |
||
| 275 | : redirect()->guest($exception->redirectTo() ?? route('login')); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Create a response object from the given validation exception. |
||
| 280 | * |
||
| 281 | * @param \Illuminate\Validation\ValidationException $e The validation exception. |
||
| 282 | * @param \Illuminate\Http\Request $request The app request. |
||
| 283 | * |
||
| 284 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 285 | */ |
||
| 286 | protected function convert_validation_exception_to_response(ValidationException $e, $request) |
||
| 287 | { |
||
| 288 | if ($e->response) { |
||
| 289 | return $e->response; |
||
| 290 | } |
||
| 291 | |||
| 292 | return $request->expectsJson() |
||
| 293 | ? $this->invalid_json($request, $e) |
||
| 294 | : $this->invalid($request, $e); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Convert a validation exception into a response. |
||
| 299 | * |
||
| 300 | * @param \Illuminate\Http\Request $request The app request. |
||
| 301 | * @param \Illuminate\Validation\ValidationException $exception The validation exception. |
||
| 302 | * |
||
| 303 | * @return \Illuminate\Http\Response |
||
| 304 | */ |
||
| 305 | protected function invalid($request, ValidationException $exception) |
||
| 306 | { |
||
| 307 | return redirect($exception->redirectTo ?? url()->previous()) |
||
| 308 | ->withInput(Arr::except($request->input(), $this->dont_flash)) |
||
| 309 | ->withErrors($exception->errors(), $exception->errorBag); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Convert a validation exception into a JSON response. |
||
| 314 | * |
||
| 315 | * @param \Illuminate\Http\Request $request The app request. |
||
| 316 | * @param \Illuminate\Validation\ValidationException $exception The validation exception. |
||
| 317 | * |
||
| 318 | * @return \Illuminate\Http\JsonResponse |
||
| 319 | */ |
||
| 320 | protected function invalid_json($request, ValidationException $exception) |
||
| 321 | { |
||
| 322 | return response()->json( |
||
| 323 | [ |
||
| 324 | 'message' => $exception->getMessage(), |
||
| 325 | 'errors' => $exception->errors(), |
||
| 326 | ], |
||
| 327 | $exception->status |
||
| 328 | ); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Prepare a response for the given exception. |
||
| 333 | * |
||
| 334 | * @param \Illuminate\Http\Request $request The app request. |
||
| 335 | * @param \Throwable $e The throwable exception. |
||
| 336 | * |
||
| 337 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 338 | */ |
||
| 339 | protected function prepare_response($request, Throwable $e) |
||
| 340 | { |
||
| 341 | if (!$this->is_http_exception($e) && $this->container['config']['app.debug']) { |
||
| 342 | return $this->to_illuminate_response($this->convert_exception_to_response($e), $e); |
||
| 343 | } |
||
| 344 | |||
| 345 | if (!$this->is_http_exception($e)) { |
||
| 346 | $e = new HttpException(500, $e->getMessage()); |
||
| 347 | } |
||
| 348 | |||
| 349 | return $this->to_illuminate_response( |
||
| 350 | $this->render_http_exception($e), |
||
| 351 | $e |
||
| 352 | ); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Create a Symfony response for the given exception. |
||
| 357 | * |
||
| 358 | * @param \Throwable $e The throwable exception. |
||
| 359 | * |
||
| 360 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 361 | */ |
||
| 362 | protected function convert_exception_to_response(Throwable $e) |
||
| 363 | { |
||
| 364 | return SymfonyResponse::create( |
||
| 365 | $this->render_exception_content($e), |
||
| 366 | $this->is_http_exception($e) ? $e->getStatusCode() : 500, |
||
| 367 | $this->is_http_exception($e) ? $e->getHeaders() : [] |
||
| 368 | ); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get the response content for the given exception. |
||
| 373 | * |
||
| 374 | * @param \Throwable $e The throwable exception. |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | protected function render_exception_content(Throwable $e) |
||
| 379 | { |
||
| 380 | try { |
||
| 381 | return $this->container['config']['app.debug'] && class_exists(Whoops::class) |
||
| 382 | ? $this->render_exception_with_whoops($e) |
||
| 383 | : $this->render_exception_with_symfony($e, $this->container['config']['app.debug']); |
||
| 384 | } catch (Exception $e) { |
||
| 385 | return $this->render_exception_with_symfony($e, $this->container['config']['app.debug']); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Render an exception to a string using "Whoops". |
||
| 391 | * |
||
| 392 | * @param \Throwable $e The throwable exception. |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | protected function render_exception_with_whoops(Throwable $e) |
||
| 397 | { |
||
| 398 | return tap( |
||
| 399 | new Whoops(), |
||
| 400 | function ($whoops) { |
||
| 401 | $whoops->appendHandler($this->whoops_handler()); |
||
| 402 | |||
| 403 | $whoops->writeToOutput(false); |
||
| 404 | |||
| 405 | $whoops->allowQuit(false); |
||
| 406 | } |
||
| 407 | )->handleException($e); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get the Whoops handler for the application. |
||
| 412 | * |
||
| 413 | * @return \Whoops\Handler\Handler |
||
| 414 | */ |
||
| 415 | protected function whoops_handler() |
||
| 416 | { |
||
| 417 | try { |
||
| 418 | return $this->container(HandlerInterface::class); |
||
| 419 | } catch (BindingResolutionException $e) { |
||
| 420 | return ( new WhoopsHandler() )->forDebug(); |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Render an exception to a string using Symfony. |
||
| 426 | * |
||
| 427 | * @param \Throwable $e The throwable exception. |
||
| 428 | * @param bool $debug Enable or disable debug. |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | protected function render_exception_with_symfony(Throwable $e, $debug) |
||
| 433 | { |
||
| 434 | $renderer = new HtmlErrorRenderer($debug); |
||
| 435 | |||
| 436 | return $renderer->getBody($renderer->render($e)); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Render the given HttpException. |
||
| 441 | * |
||
| 442 | * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e The http exception. |
||
| 443 | * |
||
| 444 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 445 | */ |
||
| 446 | protected function render_http_exception(HttpExceptionInterface $e) |
||
| 447 | { |
||
| 448 | $this->register_error_view_paths(); |
||
| 449 | $view = $this->get_http_exception_view($e); |
||
| 450 | |||
| 451 | if (view()->exists($view)) { |
||
| 452 | return response()->view( |
||
| 453 | $view, |
||
| 454 | [ |
||
| 455 | 'errors' => new ViewErrorBag(), |
||
| 456 | 'exception' => $e, |
||
| 457 | ], |
||
| 458 | $e->getStatusCode(), |
||
| 459 | $e->getHeaders() |
||
| 460 | ); |
||
| 461 | } |
||
| 462 | |||
| 463 | return $this->convert_exception_to_response($e); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Register the error template hint paths. |
||
| 468 | * |
||
| 469 | * @return void |
||
| 470 | */ |
||
| 471 | protected function register_error_view_paths() |
||
| 472 | { |
||
| 473 | $paths = collect($this->container['config']['view.paths']); |
||
| 474 | |||
| 475 | View::replaceNamespace( |
||
| 476 | 'errors', |
||
| 477 | $paths->map( |
||
| 478 | function ($path) { |
||
| 479 | return "{$path}/errors"; |
||
| 480 | } |
||
| 481 | )->push(__DIR__.'/views')->all() |
||
| 482 | ); |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Get the view used to render HTTP exceptions. |
||
| 487 | * |
||
| 488 | * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e The http exceptions. |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | protected function get_http_exception_view(HttpExceptionInterface $e) |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Map the given exception into an Illuminate response. |
||
| 499 | * |
||
| 500 | * @param \Symfony\Component\HttpFoundation\Response $response The app http response. |
||
| 501 | * @param \Throwable $e The throwable exception. |
||
| 502 | * |
||
| 503 | * @return \Illuminate\Http\Response |
||
| 504 | */ |
||
| 505 | protected function to_illuminate_response($response, Throwable $e) |
||
| 506 | { |
||
| 507 | if ($response instanceof SymfonyRedirectResponse) { |
||
| 508 | $response = new RedirectResponse( |
||
| 509 | $response->getTargetUrl(), |
||
| 510 | $response->getStatusCode(), |
||
| 511 | $response->headers->all() |
||
| 512 | ); |
||
| 513 | } else { |
||
| 514 | $response = new Response( |
||
| 515 | $response->getContent(), |
||
| 516 | $response->getStatusCode(), |
||
| 517 | $response->headers->all() |
||
| 518 | ); |
||
| 519 | } |
||
| 520 | |||
| 521 | return $response->withException($e); |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Prepare a JSON response for the given exception. |
||
| 526 | * |
||
| 527 | * @param \Illuminate\Http\Request $request The app http request. |
||
| 528 | * @param \Throwable $e The throwable exception. |
||
| 529 | * |
||
| 530 | * @return \Illuminate\Http\JsonResponse |
||
| 531 | */ |
||
| 532 | protected function prepare_json_response($request, Throwable $e) |
||
| 533 | { |
||
| 534 | return new Json_response( |
||
| 535 | $this->convert_exception_to_array($e), |
||
| 536 | $this->is_http_exception($e) ? $e->getStatusCode() : 500, |
||
| 537 | $this->is_http_exception($e) ? $e->getHeaders() : [], |
||
| 538 | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES |
||
| 539 | ); |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Convert the given exception to an array. |
||
| 544 | * |
||
| 545 | * @param \Throwable $e The throwable exception. |
||
| 546 | * |
||
| 547 | * @return array |
||
| 548 | */ |
||
| 549 | protected function convert_exception_to_array(Throwable $e) |
||
| 550 | { |
||
| 551 | return $this->container['config']['app.debug'] ? [ |
||
| 552 | 'message' => $e->getMessage(), |
||
| 553 | 'exception' => get_class($e), |
||
| 554 | 'file' => $e->getFile(), |
||
| 555 | 'line' => $e->getLine(), |
||
| 556 | 'trace' => collect($e->getTrace())->map( |
||
| 557 | function ($trace) { |
||
| 558 | return Arr::except($trace, ['args']); |
||
| 559 | } |
||
| 560 | )->all(), |
||
| 561 | ] : [ |
||
| 562 | 'message' => $this->is_http_exception($e) ? $e->getMessage() : 'Server Error', |
||
| 563 | ]; |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Render an exception to the console. |
||
| 568 | * |
||
| 569 | * @param \Symfony\Component\Console\Output\OutputInterface $output The symfony console output. |
||
| 570 | * @param \Throwable $e The throwable exception. |
||
| 571 | * |
||
| 572 | * @return void |
||
| 573 | */ |
||
| 574 | public function render_for_console($output, Throwable $e) |
||
| 575 | { |
||
| 576 | ( new ConsoleApplication() )->renderThrowable($e, $output); |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Determine if the given exception is an HTTP exception. |
||
| 581 | * |
||
| 582 | * @param \Throwable $e The throwable exception. |
||
| 583 | * |
||
| 584 | * @return bool |
||
| 585 | */ |
||
| 586 | protected function is_http_exception(Throwable $e) |
||
| 589 | } |
||
| 590 | } |
||
| 591 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths