Issues (232)

app/Exceptions/Handler.php (3 issues)

1
<?php
2
3
namespace App\Exceptions;
4
5
use App\Http\Middleware\OwnTournament;
6
use Exception;
7
use Illuminate\Auth\Access\AuthorizationException;
8
use Illuminate\Auth\AuthenticationException;
9
use Illuminate\Database\Eloquent\ModelNotFoundException;
10
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
11
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;
12
use Illuminate\Session\TokenMismatchException;
13
use Illuminate\Support\Facades\App;
14
use Illuminate\Support\Facades\Auth;
15
use Illuminate\Validation\ValidationException;
16
use Swift_TransportException;
17
use Symfony\Component\HttpKernel\Exception\HttpException;
18
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19
20
class Handler extends ExceptionHandler
21
{
22
    /**
23
     * A list of the exception types that should not be reported.
24
     *
25
     * @var array
26
     */
27
    protected $dontReport = [
28
        AuthenticationException::class,
29
        AuthorizationException::class,
30
        HttpException::class,
31
//        ModelNotFoundException::class,
32
        OwnTournament::class,
33
        AuthorizationException::class,
34
        MaintenanceModeException::class,
35
        InvitationNeededException::class,
36
        TokenMismatchException::class,
37
        ValidationException::class,
38
    ];
39
    private $sentryID;
40
41
    /**
42
     * Report or log an exception.
43
     *
44
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
45
     *
46
     * @param  \Exception $exception
47
     * @return void
48
     */
49 17
    public function report(Exception $exception)
50
    {
51 17
        if ($this->shouldReport($exception)) {
52
            $params = [];
53
            if (Auth::check()) {
54
                $params = [
55
                    'user' => [
56
                        'id' => Auth::user()->id,
57
                        'email' => Auth::user()->email
58
                    ],
59
                ];
60
            }
61
62
            $this->sentryID = app('sentry')->captureException($exception, $params);
63
        }
64 17
        parent::report($exception);
65 17
    }
66
67
    /**
68
     * Render an exception into an HTTP response.
69
     *
70
     * @param  \Illuminate\Http\Request $request
71
     * @param  \Exception $exception
72
     * @return \Symfony\Component\HttpFoundation\Response
73
     */
74 17
    public function render($request, Exception $exception)
75
    {
76 17
        if (App::environment('local')) {
0 ignored issues
show
The call to Illuminate\Support\Facades\App::environment() has too many arguments starting with 'local'. ( Ignorable by Annotation )

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

76
        if (App::/** @scrutinizer ignore-call */ environment('local')) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
77
            return parent::render($request, $exception);
78
        }
79
80
        switch ($exception) { 
81
82 17
            case $exception instanceof NotFoundHttpException:
83
84
                $code = "404";
85
                $message = "Not Found";
86
                $quote = "I will search for you through 1000 worlds and 10000 lifetimes!";
87
                $author = "Kai";
88
                $source = "47 Ronin";
89
                break;
90
91 17
            case $exception instanceof ModelNotFoundException:
92
                $code = "500";
93
                $message = "Model Not Found";
94
                $quote = "To be stupid, selfish, and have good health are three requirements for happiness, though if stupidity is lacking, all is lost.";
95
                $author = "Gustave Flaubert";
96
                $source = "";
97
                break;
98 17
            case $exception instanceof AuthorizationException:
99 9
                $code = "403";
100 9
                $message = trans('core.forbidden');
101 9
                $quote = '“And this is something I must accept - even if, like acid on metal, it is slowly corroding me inside.”';
102 9
                $author = 'Tabitha Suzuma';
103 9
                $source = trans('core.forbidden');
104 9
                break;
105
106 8
            case $exception instanceof Swift_TransportException:
107
                $code = "500";
108
                $message = $exception->getMessage();
109
                $quote = "Email can't be sent. Try again after 1000 suburis.";
110
                $author = "";
111
                $source = "";
112
                break;
113 8
            case $exception instanceof AuthenticationException:
114 4
            case $exception instanceof ValidationException:
115 7
                return parent::render($request, $exception);
116
            default:
117 1
                $code = "500";
118 1
                $message = trans('core.server_error');
119 1
                $quote = '“And this is something I must accept - even if, like acid on metal, it is slowly corroding me inside.”';
120 1
                $author = 'Tabitha Suzuma';
121 1
                $source = trans('core.forbidden');
122 1
                break;
123
        }
124
125 10
        if ($exception instanceof MaintenanceModeException)
126
            return response()->view('errors.503');
127
128
129 10
        return response()->view('errors.general',
130 10
            ['code' => $code,
131 10
                'message' => $message,
132 10
                'quote' => $quote,
133 10
                'author' => $author,
134 10
                'source' => $source,
135 10
                'sentryID' => $this->sentryID,
136
            ]
137
        );
138
    }
139
140
    /**
141
     * Convert an authentication exception into an unauthenticated response.
142
     *
143
     * @param  \Illuminate\Http\Request $request
144
     * @param  \Illuminate\Auth\AuthenticationException $exception
145
     * @return \Illuminate\Http\Response
146
     */
147 4
    protected function unauthenticated($request, AuthenticationException $exception)
148
    {
149 4
        if ($request->expectsJson()) {
150 1
            return response()->json(['error' => 'Unauthenticated.'], 401);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...nauthenticated.'), 401) also could return the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
151
        }
152
153 3
        return redirect()->guest('login');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->guest('login') returns the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
154
    }
155
}
156
//Life is growth. If we stop growing, technically and spiritually, we are as good as dead.
157
//Those who are possessed by nothing possess everything.
158
//Economy is the basis of society. When the economy is stable, society develops. The ideal economy combines the spiritual and the material, and the best commodities to trade in are sincerity and love.
159
//“There are 3 reasons for why you can't beat me. First, I'm better looking than you are. Second, your blows are too light. And third, there's nothing in the world I can't tear up.”
160
//There is truth in vine.
161
// The only true langage in the world is a Men (Musset Adaptation)