更新代码
This commit is contained in:
74
app/Api/Exceptions/Handler.php
Normal file
74
app/Api/Exceptions/Handler.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Response;
|
||||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
||||
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
|
||||
|
||||
class Handler
|
||||
{
|
||||
|
||||
public static function render($request, Exception $exception)
|
||||
{
|
||||
$errResponse = [
|
||||
'status' => 'ERROR',
|
||||
'status_code' => '404',
|
||||
'message' => $exception->getMessage(),
|
||||
];
|
||||
|
||||
if ($exception instanceof TokenExpiredException) {
|
||||
$errResponse = self::unAuthentication($request, $exception->getMessage());
|
||||
} elseif ($exception instanceof UnauthorizedHttpException) {
|
||||
$errResponse = self::unAuthentication($request, $exception->getMessage());
|
||||
} elseif ($exception instanceof MethodNotAllowedHttpException) {
|
||||
$errResponse = self::methodNotAllowed($request);
|
||||
} elseif ($exception instanceof NotFoundHttpException) {
|
||||
$errResponse = self::pageNotFound($request);
|
||||
} elseif ($exception instanceof ValidationException) {
|
||||
$errors = $exception->errors();
|
||||
$errResponse = [
|
||||
'status' => 'Validation Error',
|
||||
'status_code' => '417',
|
||||
'message' => array_shift($errors)[0],
|
||||
'api_uri' => $request->url(),
|
||||
];
|
||||
}
|
||||
|
||||
return Response::json($errResponse);
|
||||
}
|
||||
|
||||
private static function methodNotAllowed($request)
|
||||
{
|
||||
return [
|
||||
'status' => 'MethodNotAllowed',
|
||||
'status_code' => '405',
|
||||
'message' => '[ ' . $request->method() . ' ] 请求方法不允许',
|
||||
'api_uri' => $request->url(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function unAuthentication($request, $message)
|
||||
{
|
||||
return [
|
||||
'status' => 'UnAuthenticated',
|
||||
'status_code' => '401',
|
||||
'message' => '用户未登录',
|
||||
'api_uri' => $request->url(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function pageNotFound($request)
|
||||
{
|
||||
return [
|
||||
'status' => 'Api Url Not Found',
|
||||
'status_code' => '404',
|
||||
'message' => '请求的接口不存在',
|
||||
'api_uri' => $request->url(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user