Handler.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Exceptions;
  3. use BadMethodCallException;
  4. use Error;
  5. use Illuminate\Database\Eloquent\ModelNotFoundException;
  6. use Illuminate\Database\QueryException;
  7. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  8. use Illuminate\Validation\ValidationException;
  9. use InvalidArgumentException;
  10. use ParseError;
  11. use Throwable;
  12. use Tymon\JWTAuth\Exceptions\JWTException;
  13. use Tymon\JWTAuth\Exceptions\TokenBlacklistedException;
  14. use Tymon\JWTAuth\Exceptions\TokenExpiredException;
  15. use Tymon\JWTAuth\Exceptions\TokenInvalidException;
  16. class Handler extends ExceptionHandler
  17. {
  18. private int $code = 0;
  19. private string $message = Message::INTERNAL_SERVER_ERROR;
  20. private array $data = [];
  21. private int $status = Status::INTERNAL_SERVER_ERROR;
  22. /**
  23. * The list of the inputs that are never flashed to the session on validation exceptions.
  24. *
  25. * @var array<int, string>
  26. */
  27. protected $dontFlash = [
  28. 'current_password',
  29. 'password',
  30. 'password_confirmation',
  31. ];
  32. /**
  33. * Register the exception handling callbacks for the application.
  34. */
  35. public function register(): void
  36. {
  37. $this->reportable(function (Throwable $e) {
  38. //
  39. });
  40. }
  41. private function setException(Throwable $e)
  42. {
  43. if ($e instanceof ParseError) { // 语法错误
  44. $this->code = Code::PARES_ERROR;
  45. $this->message = Message::PARES_ERROR;
  46. } elseif ($e instanceof BadMethodCallException) { // 语法错误
  47. $this->code = Code::BAD_METHOD_CALL_EXCEPTION;
  48. $this->message = Message::Error;
  49. } else if ($e instanceof ApiException) { // 接口错误
  50. $this->code = $e->getCode();
  51. $this->message = $e->getMessage();
  52. }
  53. elseif ($e instanceof QueryException) { // 数据库查询错误
  54. $this->code = Code::QUERY_EXCEPTION;
  55. $this->message = Message::QUERY_EXCEPTION; // '数据库查询错误';
  56. }
  57. /*else if ($e instanceof Error) {
  58. $this->code = Code::Error;
  59. $this->message = Message::Error;
  60. }
  61. // 数据库错误
  62. // 数据库链接错误
  63. elseif ($e instanceof InvalidArgumentException) {
  64. $this->code = Code::INVALID_ARGUMENT_EXCEPTION;
  65. $this->message = Message::INVALID_ARGUMENT_EXCEPTION; // '数据库链接错误';
  66. } // 数据模型错误
  67. elseif ($e instanceof ModelNotFoundException) {
  68. $this->code = Code::MODEL_NOT_FOUND_EXCEPTION;
  69. $this->message = Message::MODEL_NOT_FOUND_EXCEPTION; // '数据库模型错误';
  70. }
  71. // 服务器错误
  72. // 映射错误
  73. elseif ($e instanceof \ReflectionException) {
  74. $this->code = Code::REFLECTION_EXCEPTION;
  75. $this->message = Message::REFLECTION_EXCEPTION; // '服务器映射错误';
  76. } // 运行时错误
  77. elseif ($e instanceof \RuntimeException) {
  78. $this->code = Code::RUNTIME_EXCEPTION;
  79. $this->message = Message::RUNTIME_EXCEPTION; // '服务器运行时错误';
  80. } // 框架运行错误
  81. elseif ($e instanceof \ErrorException) {
  82. $this->code = Code::ERROR_EXCEPTION;
  83. $this->message = Message::ERROR_EXCEPTION; // '服务器框架运行错误';
  84. }*/
  85. // 验证错误
  86. else if ($e instanceof ValidationException) {
  87. $this->code = Code::BAD_REQUEST;
  88. $this->message = array_values($e->errors())[0][0];
  89. }
  90. }
  91. public function render($request, Throwable $e)
  92. {
  93. if ($request->is('api/admin/*')) {
  94. $this->setException($e);
  95. if ($this->code) {
  96. if ($e instanceof ValidationException) {
  97. $field = array_keys($e->errors())[0];
  98. $this->data = compact('field');
  99. $this->status = Status::BAD_REQUEST;
  100. $this->message = env("APP_DEBUG") ? $this->message : Message::COMMON_EXCEPTION;
  101. } else if ($e instanceof ApiException) {
  102. $this->status = Status::OK;
  103. } else {
  104. $this->data = [
  105. 'file' => $e->getFile(),
  106. 'line' => $e->getLine()
  107. ];
  108. if (env("APP_DEBUG")) {
  109. $this->data['message'] = (($e instanceof ModelNotFoundException) ? $e->getModel() : $e->getMessage());
  110. $this->data['trace'] = $e->getTrace();
  111. } else $this->data['message'] = Message::COMMON_EXCEPTION;
  112. }
  113. }
  114. return response(['code' => $this->code, 'message' => $this->message, 'data' => $this->data], $this->status);
  115. }
  116. return parent::render($request, $e);
  117. }
  118. }