You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
4.3 KiB
152 lines
4.3 KiB
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use App\Services\ApiResponseService;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
use Throwable;
|
|
|
|
class Handler extends ExceptionHandler
|
|
{
|
|
/**
|
|
* A list of exception types with their corresponding custom log levels.
|
|
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
|
|
*/
|
|
protected $levels
|
|
= [
|
|
//
|
|
];
|
|
|
|
/**
|
|
* A list of the exception types that are not reported.
|
|
* @var array<int, class-string<\Throwable>>
|
|
*/
|
|
protected $dontReport
|
|
= [
|
|
//
|
|
];
|
|
|
|
/**
|
|
* A 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) {
|
|
//
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Render an exception into an HTTP response.
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \Throwable $e
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
* @throws \Throwable
|
|
*/
|
|
public function render($request, Throwable $e)
|
|
{
|
|
// API 异常处理
|
|
if ($request->expectsJson() || $request->is('api/*')) {
|
|
return $this->handleApiException($request, $e);
|
|
}
|
|
|
|
return parent::render($request, $e);
|
|
}
|
|
|
|
/**
|
|
* 处理 API 异常
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \Throwable $e
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
private function handleApiException(
|
|
Request $request,
|
|
Throwable $e
|
|
): JsonResponse {
|
|
// 业务异常
|
|
if ($e instanceof CustomException) {
|
|
return app(ApiResponseService::class)->businessError(
|
|
$e->getMessage(),
|
|
$e->getCode() ?: 400
|
|
);
|
|
}
|
|
|
|
// 认证异常
|
|
if ($e instanceof AuthenticationException) {
|
|
return app(ApiResponseService::class)->unauthorized(
|
|
$e->getMessage() ?: __('exception.exception_handler.login')
|
|
);
|
|
}
|
|
|
|
// 验证异常
|
|
if ($e instanceof ValidationException) {
|
|
return app(ApiResponseService::class)->businessError(
|
|
$e->getMessage(),
|
|
422,
|
|
422
|
|
);
|
|
}
|
|
|
|
// 模型未找到异常
|
|
if ($e instanceof ModelNotFoundException) {
|
|
return app(ApiResponseService::class)->businessError(
|
|
__('exception.exception_handler.resource'),
|
|
404
|
|
);
|
|
}
|
|
|
|
// 路由未找到异常
|
|
if ($e instanceof NotFoundHttpException) {
|
|
return app(ApiResponseService::class)->businessError(
|
|
__('exception.exception_handler.api'),
|
|
404
|
|
);
|
|
}
|
|
|
|
// HTTP 异常
|
|
if ($e instanceof HttpException) {
|
|
$statusCode = $e->getStatusCode();
|
|
|
|
if ($statusCode === 403) {
|
|
return app(ApiResponseService::class)->businessError(
|
|
__('exception.exception_handler.role'),
|
|
403
|
|
);
|
|
}
|
|
|
|
if ($statusCode === 429) {
|
|
return app(ApiResponseService::class)->businessError(
|
|
__('exception.exception_handler.frequent'),
|
|
429
|
|
);
|
|
}
|
|
|
|
return app(ApiResponseService::class)->systemError(
|
|
$e->getMessage() ?: __('exception.exception_handler.system')
|
|
);
|
|
}
|
|
|
|
// 其他异常
|
|
return app(ApiResponseService::class)->systemError(
|
|
__('exception.exception_handler.system') . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|