123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Exceptions;
- use App\Log;
- use App\Support\EmailQueue;
- use Exception;
- use Illuminate\Auth\AuthenticationException;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- class Handler extends ExceptionHandler
- {
- /**
- * A list of the exception types that should not be reported.
- *
- * @var array
- */
- 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,
- ];
- private $_isNoticed = false;
- /**
- * Report or log an exception.
- *
- * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
- *
- * @param \Exception $exception
- * @return void
- */
- public function report(Exception $exception)
- {
- if ($this->shouldReport($exception) && ! $this->_isNoticed) {
- // 发送异常通知
- $this->notice($exception); // sends an email
- $this->_isNoticed = true;
- }
- parent::report($exception);
- }
- /**
- * Render an exception into an HTTP response.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Exception $exception
- * @return \Illuminate\Http\Response
- */
- public function render($request, Exception $exception)
- {
- return parent::render($request, $exception);
- }
- /**
- * Convert an authentication exception into an unauthenticated response.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Illuminate\Auth\AuthenticationException $exception
- * @return \Illuminate\Http\Response
- */
- protected function unauthenticated($request, AuthenticationException $exception)
- {
- if ($request->expectsJson()) {
- return response()->json(['error' => 'Unauthenticated.'], 401);
- }
- return redirect()->guest(route('login'));
- }
- /**
- * 发送异常通知
- * @param Exception $exception
- */
- public function notice(Exception $exception)
- {
- try {
- $subject = config('app.name') . '于' . date('d日H:i:s') . '抛出异常';
- $content = $exception->getMessage() . "\r\n\r\n" . $exception->getTraceAsString();
- Log::logError($subject, ['message' => $content], 'exception');
- if (config('app.env') === 'production') {
- $email = env('EXCEPTION_REPORT_EMAIL');
- if (empty($email)) {
- return;
- }
- $emailList = array_values(array_unique(array_filter(array_map('trim', explode(',', $email)))));
- EmailQueue::rPush($subject, nl2br($content), $emailList, config('app.name'));
- }
- } catch (\Throwable $e) {
- Log::logError('发送异常通知失败!', [
- 'message' => $e->getMessage() . "\r\n" . $e->getTraceAsString()
- ], 'exception');
- // print_r($e->getMessage());
- }
- }
- }
|