123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace App\Exceptions;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Throwable;
- use Exception;
- class Handler extends ExceptionHandler
- {
- /**
- * A list of the exception types that are not reported.
- *
- * @var array<int, class-string<Throwable>>
- */
- protected $dontReport = [
- ApiException::class,
- ];
- /**
- * A list of the inputs that are never flashed for validation exceptions.
- *
- * @var array<int, string>
- */
- protected $dontFlash = [
- 'current_password',
- 'password',
- 'password_confirmation',
- ];
- /**
- * @param Throwable $e
- * @return void
- * @throws Throwable
- */
- public function report(Throwable $e)
- {
- $appEnv = env('APP_ENV', 'production');
- $appName = env('APP_NAME', '未设置项目名');
- if ($appEnv === 'production' && $this->shouldReport($e)) {
- $date = date('Y-m-d H:i:s');
- $file = $e->getFile();
- $line = $e->getLine();
- $message = $e->getMessage();
- $trace = $e->getTraceAsString();
- $traceArr = explode('#', $trace);
- $traceSimple = $trace;
- if (is_array($traceArr)) {
- $traceSub = array_slice($traceArr, 0, 3);
- $traceSimple = implode('#', $traceSub);
- }
- $msg = <<<EOF
- 项目:$appName [$appEnv]
- 报错时间:$date
- 报错文件:$file
- 报错行数:line $line
- 报错信息:$message
- 报错跟踪:
- $traceSimple
- EOF;
- sendNotice($msg);
- }
- parent::report($e);
- }
- /**
- * Register the exception handling callbacks for the application.
- *
- * @return void
- */
- public function register()
- {
- $this->reportable(function (Throwable $e) {
- //
- });
- $this->renderable(function(Exception $e, $request) {
- return $this->handleException($request, $e);
- });
- }
- private function handleException($request, Exception $e)
- {
- if ($e instanceof ApiException) {
- $data = [
- 'code' => $e->getCode(),
- 'msg' => $e->getMessage(),
- 'data' => (Object)[]
- ];
- return response()->json($data);
- }
- return null;
- }
- }
|