Issues (61)

app/Exceptions/Handler.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Gameap\Exceptions;
4
5
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
6
use Illuminate\Http\Response;
7
use Symfony\Component\HttpKernel\Exception\HttpException;
8
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
9
10
class Handler extends ExceptionHandler
11
{
12
    private const MAP_EXCEPTION_HTTP_CODE = [
13
        ValidationException::class                                           => Response::HTTP_UNPROCESSABLE_ENTITY,
14
        \Gameap\Exceptions\GdaemonAPI\InvalidApiKeyException::class          => Response::HTTP_UNAUTHORIZED,
15
        \Gameap\Exceptions\GdaemonAPI\InvalidTokenExeption::class            => Response::HTTP_UNAUTHORIZED,
16
        \Gameap\Exceptions\Repositories\RecordExistExceptions::class         => Response::HTTP_UNPROCESSABLE_ENTITY,
17
        \Gameap\Exceptions\Repositories\RepositoryValidationException::class => Response::HTTP_BAD_REQUEST,
18
        \Illuminate\Validation\ValidationException::class                    => Response::HTTP_UNPROCESSABLE_ENTITY,
19
        \Laravel\Sanctum\Exceptions\MissingAbilityException::class           => Response::HTTP_FORBIDDEN,
20
    ];
21
22
    public function render($request, \Throwable $exception)
23
    {
24
        if ($request->expectsJson() || $request->isJson()) {
25
            return $this->renderJson($request, $exception);
26
        }
27
28
        if ($exception instanceof \Gameap\Exceptions\GdaemonAPI\InvalidSetupTokenExeption) {
29
            if (app()->has('debugbar')) {
30
                app('debugbar')->disable();
0 ignored issues
show
The method disable() 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 ignore-call  annotation

30
                app('debugbar')->/** @scrutinizer ignore-call */ disable();

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
            // Return bash
34
            return response()->make('echo "' . $exception->getMessage() . '"', 401);
35
        }
36
37
        return parent::render($request, $exception);
38
    }
39 27
40
    private function renderJson($request, \Throwable $exception)
41 27
    {
42 27
        foreach (self::MAP_EXCEPTION_HTTP_CODE as $instance => $httpCode) {
43
            if ($exception instanceof $instance) {
44
                return response()->json([
45
                    'message'   => $exception->getMessage(),
46
                    'http_code' => $httpCode,
47
                ], $httpCode);
48
            }
49
        }
50
51
        if ($exception instanceof HttpExceptionInterface) {
52 27
            return response()->json([
53
                'message'   => $exception->getMessage(),
54 27
                'http_code' => $exception->getStatusCode(),
55
            ], $exception->getStatusCode(), $exception->getHeaders());
56
        }
57 27
58
        if ($exception instanceof \Symfony\Component\HttpKernel\Exception\HttpException) {
59
            return response()->json([
60
                'message'   => $exception->getMessage(),
61
                'http_code' => $exception->getStatusCode(),
62
            ], $exception->getStatusCode(), $exception->getHeaders());
63
        }
64
65
        if ($exception instanceof \Illuminate\Auth\Access\AuthorizationException) {
66
            return response()->json([
67 27
                'message'   => $exception->getMessage(),
68
                'http_code' => Response::HTTP_FORBIDDEN,
69
            ], Response::HTTP_FORBIDDEN);
70
        }
71
72
        return parent::render($request, $exception);
73
    }
74
}
75