| xqd
@@ -3,6 +3,8 @@
|
|
|
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
|
| xqd
@@ -38,4 +40,36 @@ class Handler extends ExceptionHandler
|
|
|
//
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @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, '系统错误,请稍后重试');
|
|
|
+ }
|
|
|
}
|