优惠券小程序

Handler.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Log;
  4. use App\Support\EmailQueue;
  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. ];
  17. private $_isNoticed = false;
  18. /**
  19. * A list of the inputs that are never flashed for validation exceptions.
  20. *
  21. * @var array<int, string>
  22. */
  23. protected $dontFlash = [
  24. 'current_password',
  25. 'password',
  26. 'password_confirmation',
  27. ];
  28. /**
  29. * Register the exception handling callbacks for the application.
  30. *
  31. * @return void
  32. */
  33. public function register()
  34. {
  35. $this->reportable(function (Throwable $e) {
  36. });
  37. }
  38. /**
  39. * 发送异常通知
  40. * @param Throwable $exception
  41. */
  42. public function notice(Throwable $exception)
  43. {
  44. try {
  45. $subject = config('app.name') . '于' . date('d日H:i:s') . '抛出异常';
  46. $content = $exception->getMessage() . "\r\n\r\n" . $exception->getTraceAsString();
  47. Log::error($subject, ['message' => $content], 'exception');
  48. if (config('app.env') === 'production') {
  49. $email = env('EXCEPTION_REPORT_EMAIL');
  50. if (empty($email)) {
  51. return;
  52. }
  53. $emailList = array_values(array_unique(array_filter(array_map('trim', explode(',', $email)))));
  54. EmailQueue::rPush($subject, nl2br($content), $emailList, config('app.name'));
  55. }
  56. } catch (\Throwable $e) {
  57. Log::error('发送异常通知失败!', [
  58. 'message' => $e->getMessage() . "\r\n" . $e->getTraceAsString()
  59. ], 'exception');
  60. // print_r($e->getMessage());
  61. }
  62. }
  63. // 上报异常至错误driver,如日志文件(storage/logs/laravel.log),第三方日志存储分析平台
  64. public function report(Throwable $exception)
  65. {
  66. if ($this->shouldReport($exception) && ! $this->_isNoticed) {
  67. // 发送异常通知
  68. $this->notice($exception); // sends an email
  69. $this->_isNoticed = true;
  70. }
  71. parent::report($exception);
  72. }
  73. /**
  74. * Render an exception into an HTTP response.
  75. *
  76. * @param \Illuminate\Http\Request $request
  77. * @param \Exception $exception
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function render($request, Throwable $exception)
  81. {
  82. return parent::render($request, $exception);
  83. }
  84. }