12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Exceptions;
- use App\Support\EmailQueue;
- use App\Support\Log;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Throwable;
- class Handler extends ExceptionHandler
- {
- /**
- * A list of the exception types that are not reported.
- *
- * @var array<int, class-string<Throwable>>
- */
- protected $dontReport = [
- //
- \Illuminate\Auth\AuthenticationException::class,
- \Illuminate\Auth\Access\AuthorizationException::class,
- \Symfony\Component\HttpKernel\Exception\HttpException::class,
- \Illuminate\Database\Eloquent\ModelNotFoundException::class,
- \Illuminate\Session\TokenMismatchException::class,
- \Illuminate\Validation\ValidationException::class,
- ];
- /**
- * A list of the inputs that are never flashed for validation exceptions.
- *
- * @var array<int, string>
- */
- protected $dontFlash = [
- 'current_password',
- 'password',
- 'password_confirmation',
- ];
- private $_isNoticed = false;
- /**
- * Register the exception handling callbacks for the application.
- *
- * @return void
- */
- public function register()
- {
- $this->reportable(function (Throwable $e) {
- //
- });
- }
- public function report(Throwable $e)
- {
- if ($this->shouldReport($e) && ! $this->_isNoticed) {
- // 发送异常通知
- $this->notice($e); // sends an email
- $this->_isNoticed = true;
- }
- parent::report($e);
- }
- public function notice(Throwable $e)
- {
- try {
- $subject = config('app.name') . '于' . date('d日H:i:s') . '抛出异常';
- $content = $e->getMessage() . "\r\n\r\n" . $e->getTraceAsString();
- Log::error($subject, ['message' => $content], 'exception');
- if (config('app.env') === 'production') {
- $emails = env('EXCEPTION_REPORT_EMAIL');
- if (empty($emails)) {
- return;
- }
- $emailList = array_values(array_unique(array_filter(array_map('trim', explode(',', $emails)))));
- EmailQueue::rPush($subject, nl2br($content), $emailList, config('app.name'));
- }
- } catch (\Throwable $e) {
- Log::error('发送异常通知失败!', [
- 'message' => $e->getMessage() . "\r\n" . $e->getTraceAsString()
- ], 'exception');
- }
- }
- }
|