squadron-api /
base
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Squadron\Base\Exceptions; |
||||||
| 4 | |||||||
| 5 | use Illuminate\Http\Request; |
||||||
| 6 | use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
||||||
| 7 | use Squadron\Base\Helpers\ApiResponse; |
||||||
| 8 | use Illuminate\Auth\Access\AuthorizationException; |
||||||
| 9 | use Illuminate\Auth\AuthenticationException; |
||||||
| 10 | use Illuminate\Validation\ValidationException; |
||||||
| 11 | |||||||
| 12 | class Handler extends ExceptionHandler |
||||||
| 13 | { |
||||||
| 14 | protected $dontReport = [ |
||||||
| 15 | '\League\OAuth2\Server\Exception\OAuthServerException', |
||||||
| 16 | ]; |
||||||
| 17 | |||||||
| 18 | /** |
||||||
| 19 | * Report or log an exception. |
||||||
| 20 | * This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
||||||
| 21 | * |
||||||
| 22 | * @param \Exception $exception |
||||||
| 23 | * |
||||||
| 24 | * @throws \Exception |
||||||
| 25 | */ |
||||||
| 26 | public function report(\Exception $exception): void |
||||||
| 27 | { |
||||||
| 28 | if ($this->shouldReport($exception) && ! app()->isLocal() && ! app()->runningUnitTests() && app()->bound('sentry')) |
||||||
|
0 ignored issues
–
show
introduced
by
Loading history...
The method
runningUnitTests() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 29 | { |
||||||
| 30 | app('sentry')->captureException($exception); |
||||||
|
0 ignored issues
–
show
The method
captureException() does not exist on Illuminate\Contracts\Foundation\Application.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 31 | } |
||||||
| 32 | |||||||
| 33 | parent::report($exception); |
||||||
| 34 | } |
||||||
| 35 | |||||||
| 36 | /** |
||||||
| 37 | * Render an exception into an HTTP response. |
||||||
| 38 | * |
||||||
| 39 | * @param Request $request |
||||||
| 40 | * @param \Exception $exception |
||||||
| 41 | * |
||||||
| 42 | * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response |
||||||
| 43 | */ |
||||||
| 44 | public function render($request, \Exception $exception) |
||||||
| 45 | { |
||||||
| 46 | // Define the message |
||||||
| 47 | $message = 'Sorry, something went wrong.'; |
||||||
| 48 | $data = []; |
||||||
| 49 | |||||||
| 50 | // If the app is in debug mode |
||||||
| 51 | if (config('app.debug')) |
||||||
| 52 | { |
||||||
| 53 | // Add the exception class name, message and stack trace to response |
||||||
| 54 | $data['exception'] = [ |
||||||
| 55 | 'class' => get_class($exception), |
||||||
| 56 | 'message' => $exception->getMessage(), |
||||||
| 57 | 'file' => $exception->getFile(), |
||||||
| 58 | 'line' => $exception->getLine(), |
||||||
| 59 | 'trace' => $exception->getTrace(), |
||||||
| 60 | ]; |
||||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | // Default response of 400 |
||||||
| 64 | $exceptionCode = (int) $exception->getCode(); |
||||||
| 65 | $status = $exceptionCode > 400 && $exceptionCode < 600 ? $exceptionCode : 400; |
||||||
| 66 | |||||||
| 67 | if ($this->isHttpException($exception)) |
||||||
| 68 | { |
||||||
| 69 | $status = $exception->getStatusCode(); |
||||||
|
0 ignored issues
–
show
The method
getStatusCode() does not exist on Exception. It seems like you code against a sub-type of Exception such as Symfony\Component\HttpKe...Exception\HttpException.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 70 | $message = $exception->getMessage(); |
||||||
| 71 | } |
||||||
| 72 | elseif ($exception instanceof ValidationException) |
||||||
| 73 | { |
||||||
| 74 | $status = $exception->status; |
||||||
| 75 | $message = 'Input data is invalid.'; |
||||||
| 76 | $data['errors'] = $exception->errors(); |
||||||
| 77 | } |
||||||
| 78 | elseif ($exception instanceof AuthorizationException) |
||||||
| 79 | { |
||||||
| 80 | return ApiResponse::errorAccess('This action is unauthorized.'); |
||||||
| 81 | } |
||||||
| 82 | elseif ($exception instanceof AuthenticationException) |
||||||
| 83 | { |
||||||
| 84 | return ApiResponse::errorAccess('Unauthenticated.'); |
||||||
| 85 | } |
||||||
| 86 | |||||||
| 87 | // Return a JSON response with the response array and status code |
||||||
| 88 | return ApiResponse::error($message, $status, $data); |
||||||
| 89 | } |
||||||
| 90 | } |
||||||
| 91 |