ExceptionHandler.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php namespace App\Infrastructure;
  2. use Exception;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Component\Debug\ExceptionHandler as SymfonyDisplayer;
  5. use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
  6. class ExceptionHandler implements ExceptionHandlerContract {
  7. /**
  8. * The log implementation.
  9. *
  10. * @var \Psr\Log\LoggerInterface
  11. */
  12. protected $log;
  13. /**
  14. * Create a new exception handler instance.
  15. *
  16. * @param \Psr\Log\LoggerInterface $log
  17. * @return void
  18. */
  19. public function __construct(LoggerInterface $log)
  20. {
  21. $this->log = $log;
  22. }
  23. /**
  24. * Report or log an exception.
  25. *
  26. * @param \Exception $e
  27. * @return void
  28. */
  29. public function report(Exception $e)
  30. {
  31. $this->log->error((string) $e);
  32. }
  33. /**
  34. * Render an exception into a response.
  35. *
  36. * @param \Illuminate\Http\Request $request
  37. * @param \Exception $e
  38. * @return \Symfony\Component\HttpFoundation\Response
  39. */
  40. public function render($request, Exception $e)
  41. {
  42. return (new SymfonyDisplayer)->createResponse($e);
  43. }
  44. /**
  45. * Render an exception to the console.
  46. *
  47. * @param \Symfony\Component\Console\Output\OutputInterface $output
  48. * @param \Exception $e
  49. * @return void
  50. */
  51. public function renderForConsole($output, Exception $e)
  52. {
  53. $output->writeln((string) $e);
  54. }
  55. }