75 lines
2.4 KiB
PHP
75 lines
2.4 KiB
PHP
<?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(),
|
|
];
|
|
}
|
|
}
|