新版订单消耗系统

Handler.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Log;
  4. use App\Support\EmailQueue;
  5. use Exception;
  6. use Illuminate\Auth\AuthenticationException;
  7. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  8. class Handler extends ExceptionHandler
  9. {
  10. /**
  11. * A list of the exception types that should not be reported.
  12. *
  13. * @var array
  14. */
  15. protected $dontReport = [
  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. private $_isNoticed = false;
  24. /**
  25. * Report or log an exception.
  26. *
  27. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  28. *
  29. * @param \Exception $exception
  30. * @return void
  31. */
  32. public function report(Exception $exception)
  33. {
  34. if ($this->shouldReport($exception) && ! $this->_isNoticed) {
  35. // 发送异常通知
  36. $this->notice($exception); // sends an email
  37. $this->_isNoticed = true;
  38. }
  39. parent::report($exception);
  40. }
  41. /**
  42. * Render an exception into an HTTP response.
  43. *
  44. * @param \Illuminate\Http\Request $request
  45. * @param \Exception $exception
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function render($request, Exception $exception)
  49. {
  50. return parent::render($request, $exception);
  51. }
  52. /**
  53. * Convert an authentication exception into an unauthenticated response.
  54. *
  55. * @param \Illuminate\Http\Request $request
  56. * @param \Illuminate\Auth\AuthenticationException $exception
  57. * @return \Illuminate\Http\Response
  58. */
  59. protected function unauthenticated($request, AuthenticationException $exception)
  60. {
  61. if ($request->expectsJson()) {
  62. return response()->json(['error' => 'Unauthenticated.'], 401);
  63. }
  64. return redirect()->guest(route('login'));
  65. }
  66. /**
  67. * 发送异常通知
  68. * @param Exception $exception
  69. */
  70. public function notice(Exception $exception)
  71. {
  72. try {
  73. $subject = config('app.name') . '于' . date('d日H:i:s') . '抛出异常';
  74. $content = $exception->getFile().'('.$exception->getLine().'):'.$exception->getMessage() . "\r\n\r\n" . $exception->getTraceAsString();
  75. Log::logError($subject, ['message' => $content], 'exception');
  76. if (config('app.env') === 'production') {
  77. $email = env('EXCEPTION_REPORT_EMAIL');
  78. if (empty($email)) {
  79. return;
  80. }
  81. $emailList = array_values(array_unique(array_filter(array_map('trim', explode(',', $email)))));
  82. EmailQueue::rPush($subject, nl2br($content), $emailList, config('app.name'));
  83. }
  84. } catch (\Throwable $e) {
  85. // Log::logError('发送异常通知失败!', [
  86. // 'message' => $e->getMessage() . "\r\n" . $e->getTraceAsString()
  87. // ], 'exception');
  88. // print_r($e->getMessage());
  89. }
  90. }
  91. }