diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 97b1078e..6ae08353 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -26,6 +26,10 @@ return (new Config()) 'no_unused_imports' => true, 'no_whitespace_before_comma_in_array' => true, 'object_operator_without_whitespace' => true, + 'ordered_imports' => [ + 'imports_order' => ['class', 'function', 'const'], + 'sort_algorithm' => 'alpha', + ], 'single_quote' => true, 'trailing_comma_in_multiline' => [ 'elements' => ['arrays'], diff --git a/app/Exceptions/BizException.php b/app/Exceptions/BizException.php new file mode 100644 index 00000000..f4d3520c --- /dev/null +++ b/app/Exceptions/BizException.php @@ -0,0 +1,37 @@ +status = $status; + + return $this; + } + + /** + * 报告异常 + * + * @return mixed + */ + public function report() + { + } +} diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 3ca4b345..0982dad3 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -2,7 +2,11 @@ namespace App\Exceptions; +use Illuminate\Auth\AuthenticationException; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; +use Illuminate\Validation\ValidationException; +use Symfony\Component\HttpKernel\Exception\HttpException; use Throwable; class Handler extends ExceptionHandler @@ -34,8 +38,78 @@ class Handler extends ExceptionHandler */ public function register() { - $this->reportable(function (Throwable $e) { - // + $this->map(ModelNotFoundException::class, function ($e) { + $resource = __($key = 'models.'.$e->getModel()); + + if ($key === $resource) { + $resource = str_ireplace(['App\\Models\\Admin\\', 'App\\Models\\'], '', $e->getModel()); + } + + return new HttpException(404, __(':resource not found', ['resource' => $resource]), $e); + }); + + $this->renderable(function (BizException $e, $request) { + if ($this->shouldReturnJson($request, $e)) { + return response()->json($this->convertExceptionToArray($e), $e->status); + } + + if (! config('app.debug')) { + $e = new HttpException($e->status, $e->getMessage(), $e); + } + + return $this->prepareResponse($request, $e); }); } + + /** + * {@inheritdoc} + */ + protected function unauthenticated($request, AuthenticationException $exception) + { + return $this->shouldReturnJson($request, $exception) + ? response()->json([ + 'errcode' => 401, + 'message' => $exception->getMessage(), + ], 401) + : redirect()->guest($exception->redirectTo() ?? route('login')); + } + + /** + * {@inheritdoc} + */ + protected function invalidJson($request, ValidationException $exception) + { + return response()->json([ + 'errcode' => 422, + 'message' => $exception->getMessage(), + 'errors' => $exception->errors(), + ], $exception->status); + } + + /** + * {@inheritdoc} + */ + protected function convertExceptionToArray(Throwable $e) + { + return array_merge( + parent::convertExceptionToArray($e), + $this->isBizException($e) ? [ + 'errcode' => $e->getCode(), + 'message' => $e->getMessage(), + ] : [ + 'errcode' => $this->isHttpException($e) ? $e->getStatusCode() : 500, + ] + ); + } + + /** + * 确认给定的异常是否是自定义业务异常 + * + * @param Throwable $e + * @return bool + */ + protected function isBizException(Throwable $e): bool + { + return $e instanceof BizException; + } } diff --git a/resources/lang/zh_CN.json b/resources/lang/zh_CN.json new file mode 100644 index 00000000..47363b12 --- /dev/null +++ b/resources/lang/zh_CN.json @@ -0,0 +1,3 @@ +{ + ":resource not found": ":resource 未找到" +} diff --git a/resources/lang/zh_CN/models.php b/resources/lang/zh_CN/models.php new file mode 100644 index 00000000..afd366a3 --- /dev/null +++ b/resources/lang/zh_CN/models.php @@ -0,0 +1,5 @@ + '用户', +];