Passed
Push — dev6 ( 58f07d...980583 )
by Ron
09:48
created

Handler::render()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 35
ccs 0
cts 0
cp 0
rs 8.0555
c 0
b 0
f 0
cc 9
nc 4
nop 2
crap 90
1
<?php
2
3
namespace App\Exceptions;
4
5
use Throwable;
6
use Inertia\Inertia;
7
use Illuminate\Support\Str;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Log;
10
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
11
12
class Handler extends ExceptionHandler
13
{
14
    /**
15
     * A list of the exception types that are not reported.
16
     *
17
     * @var array
18
     */
19
    protected $dontReport = [
20
        //
21
    ];
22
23
    /**
24
     * A list of the inputs that are never flashed for validation exceptions.
25
     *
26
     * @var array
27
     */
28
    protected $dontFlash = [
29 148
        'current_password',
30
        'password',
31 148
        'password_confirmation',
32
    ];
33 148
34 148
    /**
35
     * Register the exception handling callbacks for the application.
36
     *
37
     * @return void
38
     */
39
    public function register()
40
    {
41
        // $this->reportable(function (Throwable $e) {
42
        //     //
43
        // });
44
    }
45
46
    /**
47
     * Check for an error message
48
     */
49
    public function render($request, Throwable $e)
50
    {
51
        $response = parent::render($request, $e);
52
53
        if(!app()->environment(['local', 'testing']) && in_array($response->status(), [500, 503, 404, 403]))
0 ignored issues
show
introduced by
The method environment() 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 ignore-call  annotation

53
        if(!app()->/** @scrutinizer ignore-call */ environment(['local', 'testing']) && in_array($response->status(), [500, 503, 404, 403]))
Loading history...
Bug introduced by
The method status() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Illuminate\Http\Response or Illuminate\Http\JsonResponse or Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

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

53
        if(!app()->environment(['local', 'testing']) && in_array($response->/** @scrutinizer ignore-call */ status(), [500, 503, 404, 403]))
Loading history...
54
        {
55
            Log::notice('Server Error - '.$response->status().' occured.  Message - '.$response->exception->getMessage(), [
56
                'auth'     => Auth::check() ? true : false,
57
                'user_id'  => Auth::check() ? Auth::user()->user_id : null,
58
                'username' => Auth::check() ? Auth::user()->username : null,
59
            ]);
60
61
            $message = $response->exception->getMessage();
62
63
            if($message == "" || Str::startsWith($message, 'No query results'))
64
            {
65
                $message = null;
66
            }
67
68
            return Inertia::render('Error', [
69
                    'status'  => $response->status(),
70
                    'message' => $message,
71
                ])
72
                ->toResponse($request)
73
                ->setStatusCode($response->status());
74
        }
75
        else if ($response->status() === 419)
76
        {
77
            return back()->with([
78
                'message' => 'The page expired, please try again.',
79
                'type'    => 'danger',
80
            ]);
81
        }
82
83
        return $response;
84
    }
85
}
86