Passed
Push — feature/optimize ( cdf8bf...0d9399 )
by Fu
02:41
created

Handler::parserException()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 8
nop 3
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
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 Symfony\Component\HttpKernel\Exception\{
11
    HttpException, UnauthorizedHttpException
12
};
13
use Tymon\JWTAuth\Exceptions\{
14
    InvalidClaimException,
15
    JWTException,
16
    PayloadException,
17
    TokenBlacklistedException,
18
    TokenExpiredException,
19
    TokenInvalidException,
20
    UserNotDefinedException
21
};
22
23
class Handler extends ExceptionHandler
24
{
25
    const STATUS_CODE = 'status_code';
26
    const ERROR_CODE = 'error_code';
27
    const MESSAGE = 'message';
28
    const DEBUG = 'debug';
29
30
    /**
31
     * Render an exception into an HTTP response.
32
     *
33
     * @param \Illuminate\Http\Request $request
34
     * @param \Exception|HttpException $exception
35
     *
36
     * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
37
     */
38
    public function render($request, Exception $exception)
39
    {
40
        if ($request->is('api/*') || $request->wantsJson()) {
41
            if ('web' !== config('core.api.error_format')) {
42
                $response = [];
43
                $exceptionClass = get_class($exception);
44
                $exception = $this->overwrite($exception);
45
46
                if ($exception instanceof ModelNotFoundException) {
47
                    $response = $this->parserModelNotFoundException($response, $exception);
48
                } elseif ($exception instanceof JWTException) {
49
                    $response = $this->parserJWTException($response, $exception, $exceptionClass);
50
                } else {
51
                    $response = $this->parserException($response, $exception, $exceptionClass);
52
                }
53
54
                $response = $this->debug($response, $exception);
55
56
                return $this->response(collect($response)->toArray());
57
            }
58
        }
59
60
        return parent::render($request, $exception);
61
    }
62
63
    protected function response(array $response)
64
    {
65
        return (new Response($response))->render();
66
    }
67
68
    protected function debug(array $response, Exception $exception)
69
    {
70
        if (true === config('app.debug')) {
71
            $response['meta'][self::DEBUG]['file'] = $exception->getFile();
72
            $response['meta'][self::DEBUG]['line'] = $exception->getLine();
73
            $response['meta'][self::DEBUG]['trace'] = $exception->getTrace();
74
        }
75
76
        return $response;
77
    }
78
79
    protected function overwrite(Exception $exception)
80
    {
81
        if ($exception instanceof UnauthorizedHttpException && method_exists($exception, 'getPrevious')) {
82
            $exception = $exception->getPrevious();
83
        }
84
85
        return $exception;
86
    }
87
88
    protected function parserModelNotFoundException(
89
        array $response,
90
        Exception $exception
91
    ): array {
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 parserJWTException(array $response, Exception $exception, string $exceptionClass): array
99
    {
100
        switch ($exceptionClass) {
101
            case InvalidClaimException::class:
102
                $response['meta'][self::ERROR_CODE] = JWTErrorCode::INVALID_CLAIM;
103
                break;
104
105
            case PayloadException::class:
106
                $response['meta'][self::ERROR_CODE] = JWTErrorCode::PAYLOAD;
107
                break;
108
109
            case TokenBlacklistedException::class:
110
                $response['meta'][self::ERROR_CODE] = JWTErrorCode::TOKEN_BLACKLISTED;
111
                break;
112
113
            case TokenExpiredException::class:
114
                if ('Token has expired and can no longer be refreshed' === $exception->getMessage()) {
115
                    $response['meta'][self::ERROR_CODE] = JWTErrorCode::CAN_NOT_REFRESHED;
116
                } else {
117
                    $response['meta'][self::ERROR_CODE] = JWTErrorCode::TOKEN_EXPIRED;
118
                }
119
                break;
120
121
            case TokenInvalidException::class:
122
                $response['meta'][self::ERROR_CODE] = JWTErrorCode::TOKEN_INVALID;
123
                break;
124
125
            case UserNotDefinedException::class:
126
                $response['meta'][self::ERROR_CODE] = JWTErrorCode::USER_NOT_DEFINED;
127
                break;
128
129
            default:
130
                $response['meta'][self::ERROR_CODE] = JWTErrorCode::DEFAULT;;
131
        }
132
133
        $response['meta'][self::STATUS_CODE] = StatusCodeEnum::HTTP_UNAUTHORIZED;
134
        $response['meta'][self::MESSAGE] = $exception->getMessage();
135
136
        return $response;
137
    }
138
139
    protected function parserException(array $response, Exception $exception, string $exceptionClass): array
140
    {
141
        if (method_exists($exception, 'getStatusCode')) {
142
            $response['meta'][self::STATUS_CODE] = $exception->getStatusCode();
143
        } else {
144
            $response['meta'][self::STATUS_CODE] = StatusCodeEnum::HTTP_INTERNAL_SERVER_ERROR;
145
        }
146
147
        if (method_exists($exception, 'getCode')) {
148
            $response['meta'][self::ERROR_CODE] = $exception->getCode();
149
        }
150
151
        if (null === $exception->getMessage()) {
152
            $response['meta'][self::MESSAGE] = class_basename($exceptionClass);
153
        } else {
154
            $response['meta'][self::MESSAGE] = $exception->getMessage();
155
        }
156
157
        return $response;
158
    }
159
}
160