123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Exceptions;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Illuminate\Validation\ValidationException;
- use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
- use Throwable;
- class Handler extends ExceptionHandler
- {
- /**
- * A list of the exception types that are not reported.
- *
- * @var array
- */
- protected $dontReport = [
- //
- ];
- /**
- * A list of the inputs that are never flashed for validation exceptions.
- *
- * @var array
- */
- protected $dontFlash = [
- 'current_password',
- 'password',
- 'password_confirmation',
- ];
- /**
- * Register the exception handling callbacks for the application.
- *
- * @return void
- */
- public function register()
- {
- $this->reportable(function (Throwable $e) {
- //
- });
- }
- /**
- * @create JianJia.Zhou<z656123456@gmail.com>
- * @param \Illuminate\Http\Request $request
- * @param Throwable $exception
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
- */
- public function render($request, Throwable $exception)
- {
- //如果错误是 ValidationException 的一个实例,说明是一个验证的错误
- if ($exception instanceof ValidationException) {
- return out(null, 10000, array_values($exception->errors())[0][0]);
- }
- // 需要登录
- if ($exception instanceof UnauthorizedHttpException) {
- return out(null, 401, $exception->getMessage());
- }
- // 其他异常
- if ($exception instanceof Exception){
- $msg = $exception->getMessage();
- $out = json_decode($msg, true);
- //\Log::info('异常内容');
- //\Log::info($exception);
- return out($out['data'] ?? null, $out['code'] ?? 500, $out['message'] ?? $msg);
- }
- if (env('APP_DEBUG')) {
- return out(null, 500, $exception->getMessage());
- }
- return out(null, 500, '系统错误,请稍后重试');
- }
- }
|