123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace App\Exceptions;
- use BadMethodCallException;
- use Error;
- use Illuminate\Database\Eloquent\ModelNotFoundException;
- use Illuminate\Database\QueryException;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Illuminate\Validation\ValidationException;
- use InvalidArgumentException;
- use ParseError;
- use Throwable;
- use Tymon\JWTAuth\Exceptions\JWTException;
- use Tymon\JWTAuth\Exceptions\TokenBlacklistedException;
- use Tymon\JWTAuth\Exceptions\TokenExpiredException;
- use Tymon\JWTAuth\Exceptions\TokenInvalidException;
- class Handler extends ExceptionHandler
- {
- private int $code = 0;
- private string $message = Message::INTERNAL_SERVER_ERROR;
- private array $data = [];
- private int $status = Status::INTERNAL_SERVER_ERROR;
- /**
- * The list of the inputs that are never flashed to the session on validation exceptions.
- *
- * @var array<int, string>
- */
- protected $dontFlash = [
- 'current_password',
- 'password',
- 'password_confirmation',
- ];
- /**
- * Register the exception handling callbacks for the application.
- */
- public function register(): void
- {
- $this->reportable(function (Throwable $e) {
- //
- });
- }
- private function setException(Throwable $e)
- {
- if ($e instanceof ParseError) { // 语法错误
- $this->code = Code::PARES_ERROR;
- $this->message = Message::PARES_ERROR;
- } elseif ($e instanceof BadMethodCallException) { // 语法错误
- $this->code = Code::BAD_METHOD_CALL_EXCEPTION;
- $this->message = Message::Error;
- } else if ($e instanceof ApiException) { // 接口错误
- $this->code = $e->getCode();
- $this->message = $e->getMessage();
- }
- elseif ($e instanceof QueryException) { // 数据库查询错误
- $this->code = Code::QUERY_EXCEPTION;
- $this->message = Message::QUERY_EXCEPTION; // '数据库查询错误';
- }
- /*else if ($e instanceof Error) {
- $this->code = Code::Error;
- $this->message = Message::Error;
- }
- // 数据库错误
- // 数据库链接错误
- elseif ($e instanceof InvalidArgumentException) {
- $this->code = Code::INVALID_ARGUMENT_EXCEPTION;
- $this->message = Message::INVALID_ARGUMENT_EXCEPTION; // '数据库链接错误';
- } // 数据模型错误
- elseif ($e instanceof ModelNotFoundException) {
- $this->code = Code::MODEL_NOT_FOUND_EXCEPTION;
- $this->message = Message::MODEL_NOT_FOUND_EXCEPTION; // '数据库模型错误';
- }
- // 服务器错误
- // 映射错误
- elseif ($e instanceof \ReflectionException) {
- $this->code = Code::REFLECTION_EXCEPTION;
- $this->message = Message::REFLECTION_EXCEPTION; // '服务器映射错误';
- } // 运行时错误
- elseif ($e instanceof \RuntimeException) {
- $this->code = Code::RUNTIME_EXCEPTION;
- $this->message = Message::RUNTIME_EXCEPTION; // '服务器运行时错误';
- } // 框架运行错误
- elseif ($e instanceof \ErrorException) {
- $this->code = Code::ERROR_EXCEPTION;
- $this->message = Message::ERROR_EXCEPTION; // '服务器框架运行错误';
- }*/
- // 验证错误
- else if ($e instanceof ValidationException) {
- $this->code = Code::BAD_REQUEST;
- $this->message = array_values($e->errors())[0][0];
- }
- }
- public function render($request, Throwable $e)
- {
- if ($request->is('api/admin/*')) {
- $this->setException($e);
- if ($this->code) {
- if ($e instanceof ValidationException) {
- $field = array_keys($e->errors())[0];
- $this->data = compact('field');
- $this->status = Status::BAD_REQUEST;
- $this->message = env("APP_DEBUG") ? $this->message : Message::COMMON_EXCEPTION;
- } else if ($e instanceof ApiException) {
- $this->status = Status::OK;
- } else {
- $this->data = [
- 'file' => $e->getFile(),
- 'line' => $e->getLine()
- ];
- if (env("APP_DEBUG")) {
- $this->data['message'] = (($e instanceof ModelNotFoundException) ? $e->getModel() : $e->getMessage());
- $this->data['trace'] = $e->getTrace();
- } else $this->data['message'] = Message::COMMON_EXCEPTION;
- }
- }
- return response(['code' => $this->code, 'message' => $this->message, 'data' => $this->data], $this->status);
- }
- return parent::render($request, $e);
- }
- }
|