Handler::report()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Exceptions;
4
5
use Exception;
6
use Illuminate\Validation\ValidationException;
7
use Illuminate\Auth\Access\AuthorizationException;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
10
use Symfony\Component\HttpKernel\Exception\HttpException;
11
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
14
15
class Handler extends ExceptionHandler
16
{
17
    /**
18
     * A list of the exception types that should not be reported.
19
     *
20
     * @var array
21
     */
22
    protected $dontReport = [
23
        AuthorizationException::class,
24
        HttpException::class,
25
        ModelNotFoundException::class,
26
        ValidationException::class,
27
    ];
28
29
    /**
30
     * Report or log an exception.
31
     *
32
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
33
     *
34
     * @param  \Exception  $e
35
     * @return void
36
     */
37
    public function report(Exception $e)
38
    {
39
        parent::report($e);
40
    }
41
42
    /**
43
     * Render an exception into an HTTP response.
44
     *
45
     * @param  \Illuminate\Http\Request  $request
46
     * @param  \Exception  $e
47
     * @return \Illuminate\Http\Response
48
     */
49
    public function render($request, Exception $e)
50
    {
51
        if(env('APP_DEBUG')){
52
            return parent::render($request, $e);
53
        }
54
55
        if($e instanceof NotFoundHttpException){
56
            return response()->json(['message' => 'Bad Request', 'code' => 400], 400);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57
        }
58
59
        if($e instanceof MethodNotAllowedHttpException){
60
            return response()->json(['message' => 'Not Found', 'code' => 404], 404);
61
        }
62
63
        return response()->json(['message' => 'Unexpected Error', 'code' => 500], 500);
64
    }
65
}
66