Ei kuvausta

Handler.php 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Support\EmailQueue;
  4. use App\Support\Log;
  5. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  6. use Throwable;
  7. class Handler extends ExceptionHandler
  8. {
  9. /**
  10. * A list of the exception types that are not reported.
  11. *
  12. * @var array<int, class-string<Throwable>>
  13. */
  14. protected $dontReport = [
  15. //
  16. \Illuminate\Auth\AuthenticationException::class,
  17. \Illuminate\Auth\Access\AuthorizationException::class,
  18. \Symfony\Component\HttpKernel\Exception\HttpException::class,
  19. \Illuminate\Database\Eloquent\ModelNotFoundException::class,
  20. \Illuminate\Session\TokenMismatchException::class,
  21. \Illuminate\Validation\ValidationException::class,
  22. ];
  23. /**
  24. * A list of the inputs that are never flashed for validation exceptions.
  25. *
  26. * @var array<int, string>
  27. */
  28. protected $dontFlash = [
  29. 'current_password',
  30. 'password',
  31. 'password_confirmation',
  32. ];
  33. private $_isNoticed = false;
  34. /**
  35. * Register the exception handling callbacks for the application.
  36. *
  37. * @return void
  38. */
  39. public function register()
  40. {
  41. $this->reportable(function (Throwable $e) {
  42. //
  43. });
  44. }
  45. public function report(Throwable $e)
  46. {
  47. if ($this->shouldReport($e) && ! $this->_isNoticed) {
  48. // 发送异常通知
  49. $this->notice($e); // sends an email
  50. $this->_isNoticed = true;
  51. }
  52. parent::report($e);
  53. }
  54. public function notice(Throwable $e)
  55. {
  56. try {
  57. $subject = config('app.name') . '于' . date('d日H:i:s') . '抛出异常';
  58. $content = $e->getMessage() . "\r\n\r\n" . $e->getTraceAsString();
  59. Log::error($subject, ['message' => $content], 'exception');
  60. if (config('app.env') === 'production') {
  61. $emails = env('EXCEPTION_REPORT_EMAIL');
  62. if (empty($emails)) {
  63. return;
  64. }
  65. $emailList = array_values(array_unique(array_filter(array_map('trim', explode(',', $emails)))));
  66. EmailQueue::rPush($subject, nl2br($content), $emailList, config('app.name'));
  67. }
  68. } catch (\Throwable $e) {
  69. Log::error('发送异常通知失败!', [
  70. 'message' => $e->getMessage() . "\r\n" . $e->getTraceAsString()
  71. ], 'exception');
  72. }
  73. }
  74. }