抖音小程序

Handler.php 2.4KB

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