1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Core\Supports; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
7
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
8
|
|
|
use Modules\Core\Enums\StatusCodeEnum; |
9
|
|
|
use Modules\Core\ErrorCodes\JWTErrorCode; |
10
|
|
|
use Modules\Core\Traits\Supports\HandleParseTrait; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\{ |
12
|
|
|
HttpException, UnauthorizedHttpException |
13
|
|
|
}; |
14
|
|
|
use Tymon\JWTAuth\Exceptions\{ |
15
|
|
|
InvalidClaimException, |
16
|
|
|
JWTException, |
17
|
|
|
PayloadException, |
18
|
|
|
TokenBlacklistedException, |
19
|
|
|
TokenExpiredException, |
20
|
|
|
TokenInvalidException, |
21
|
|
|
UserNotDefinedException |
22
|
|
|
}; |
23
|
|
|
|
24
|
|
|
class Handler extends ExceptionHandler |
25
|
|
|
{ |
26
|
|
|
const STATUS_CODE = 'status_code'; |
27
|
|
|
const ERROR_CODE = 'error_code'; |
28
|
|
|
const MESSAGE = 'message'; |
29
|
|
|
const DEBUG = 'debug'; |
30
|
|
|
|
31
|
|
|
use HandleParseTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Render an exception into an HTTP response. |
35
|
|
|
* |
36
|
|
|
* @param \Illuminate\Http\Request $request |
37
|
|
|
* @param \Exception|HttpException $exception |
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response |
40
|
|
|
*/ |
41
|
|
|
public function render($request, Exception $exception) |
42
|
|
|
{ |
43
|
|
|
if ($request->is('api/*') || $request->wantsJson()) { |
44
|
|
|
if ('web' !== config('core.api.error_format')) { |
45
|
|
|
$response = []; |
46
|
|
|
$exception = $this->parseUnauthorizedHttpException($exception); |
47
|
|
|
|
48
|
|
|
if ($exception instanceof ModelNotFoundException) { |
49
|
|
|
$response = $this->parseModelNotFoundException($response, $exception); |
50
|
|
|
} elseif ($exception instanceof JWTException) { |
51
|
|
|
$response = $this->parseJWTException($response, $exception); |
52
|
|
|
} else { |
53
|
|
|
$response = $this->parseException($response, $exception, get_class($exception)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$response = $this->parseDebug($response, $exception); |
57
|
|
|
|
58
|
|
|
return $this->response(collect($response)->toArray()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return parent::render($request, $exception); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function response(array $response) |
66
|
|
|
{ |
67
|
|
|
return (new Response($response))->render(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function parseDebug(array $response, Exception $exception) |
71
|
|
|
{ |
72
|
|
|
if (true === config('app.debug')) { |
73
|
|
|
$response['meta'][self::DEBUG]['file'] = $exception->getFile(); |
74
|
|
|
$response['meta'][self::DEBUG]['line'] = $exception->getLine(); |
75
|
|
|
$response['meta'][self::DEBUG]['trace'] = $exception->getTrace(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $response; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function parseUnauthorizedHttpException(Exception $exception) |
82
|
|
|
{ |
83
|
|
|
if ($exception instanceof UnauthorizedHttpException && method_exists($exception, 'getPrevious')) { |
84
|
|
|
$exception = $exception->getPrevious(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $exception; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function parseModelNotFoundException(array $response, Exception $exception): array |
91
|
|
|
{ |
92
|
|
|
$response['meta'][self::STATUS_CODE] = StatusCodeEnum::HTTP_NOT_FOUND; |
93
|
|
|
$response['meta'][self::MESSAGE] = $exception->getMessage(); |
94
|
|
|
|
95
|
|
|
return $response; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function parseException(array $response, Exception $exception, string $exceptionClass): array |
99
|
|
|
{ |
100
|
|
|
if (method_exists($exception, 'getStatusCode')) { |
101
|
|
|
$response['meta'][self::STATUS_CODE] = $exception->getStatusCode(); |
102
|
|
|
} else { |
103
|
|
|
$response['meta'][self::STATUS_CODE] = StatusCodeEnum::HTTP_INTERNAL_SERVER_ERROR; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (method_exists($exception, 'getCode')) { |
107
|
|
|
$response['meta'][self::ERROR_CODE] = $exception->getCode(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (null === $exception->getMessage()) { |
111
|
|
|
$response['meta'][self::MESSAGE] = class_basename($exceptionClass); |
112
|
|
|
} else { |
113
|
|
|
$response['meta'][self::MESSAGE] = $exception->getMessage(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $response; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|