Issues (115)

src/Exceptions/JsonApiHandler.php (4 issues)

1
<?php
2
3
namespace VGirol\JsonApi\Exceptions;
4
5
use Exception;
6
use Illuminate\Contracts\Container\Container;
7
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
use Throwable;
10
use VGirol\JsonApi\Services\ResponseService;
11
12
class JsonApiHandler extends ExceptionHandler
13
{
14
    /**
15
     * Undocumented variable
16
     *
17
     * @var ResponseService
18
     */
19
    protected $responseService;
20
21
    /**
22
     * A list of the exception types that are not reported.
23
     *
24
     * @var array
25
     */
26
    protected $dontReport = [];
27
28
    /**
29
     * A list of the inputs that are never flashed for validation exceptions.
30
     *
31
     * @var array
32
     */
33
    protected $dontFlash = [
34
        'password',
35
        'password_confirmation',
36
    ];
37
38
    /**
39
     * Create a new exception handler instance.
40
     *
41
     * @param Container $container
42
     * @param ResponseService $responseService
43
     *
44
     * @return void
45
     */
46
    public function __construct(Container $container, ResponseService $responseService)
47
    {
48
        parent::__construct($container);
49
        $this->responseService = $responseService;
50
    }
51
52
    /**
53
     * Report or log an exception.
54
     *
55
     * @param \Throwable $exception
56
     *
57
     * @return void
58
     */
59
    public function report(Throwable $exception)
60
    {
61
        parent::report($exception);
62
    }
63
64
    /**
65
     * Render an exception into an HTTP response.
66
     *
67
     * @param \Illuminate\Http\Request $request
68
     * @param \Throwable               $exception
69
     *
70
     * @return \Illuminate\Http\Response
71
     */
72
    public function render($request, Throwable $exception)
73
    {
74
        if ($request->wantsJson()) {   // has Accept: application/json in request
0 ignored issues
show
The method wantsJson() does not exist on Illuminate\Http\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        if ($request->/** @scrutinizer ignore-call */ wantsJson()) {   // has Accept: application/json in request

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...
75
            return $this->handleApiException($request, $exception);
76
        }
77
78
        return parent::render($request, $exception);
79
    }
80
81
    /**
82
     * Undocumented function
83
     *
84
     * @param Request   $request
0 ignored issues
show
The type VGirol\JsonApi\Exceptions\Request was not found. Did you mean Request? If so, make sure to prefix the type with \.
Loading history...
85
     * @param Throwable $exception
86
     *
87
     * @return JsonResponse
0 ignored issues
show
The type VGirol\JsonApi\Exceptions\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
88
     */
89
    protected function handleApiException($request, Throwable $exception)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

89
    protected function handleApiException(/** @scrutinizer ignore-unused */ $request, Throwable $exception)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        $exception = $this->prepareException($exception);
92
        jsonapiError()->addException($exception, false);
93
94
        return $this->responseService->createErrorResponse();
95
    }
96
97
    /**
98
     * Prepare exception for rendering.
99
     *
100
     * @param \Throwable $e
101
     *
102
     * @return \Throwable
103
     */
104
    protected function prepareException(Throwable $e)
105
    {
106
        $e = parent::prepareException($e);
107
108
        if ($e instanceof NotFoundHttpException) {
109
            $e = new JsonApi404Exception($e->getMessage(), 0, $e);
110
        }
111
112
        if (method_exists($e, 'prepareException')) {
113
            call_user_func([$e, 'prepareException']);
114
        }
115
116
        return $e;
117
    }
118
}
119