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