/
Handler.php
185 lines (159 loc) · 5.17 KB
/
Handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Copyright 2015-2017 ppy Pty. Ltd.
*
* This file is part of osu!web. osu!web is distributed with the hope of
* attracting more community contributions to the core ecosystem of osu!.
*
* osu!web is free software: you can redistribute it and/or modify
* it under the terms of the Affero GNU General Public License version 3
* as published by the Free Software Foundation.
*
* osu!web is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with osu!web. If not, see <http://www.gnu.org/licenses/>.
*/
namespace App\Exceptions;
use App;
use Auth;
use Exception;
use Illuminate\Auth\Access\AuthorizationException as LaravelAuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Session\TokenMismatchException;
use Sentry;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
// laravel's
AuthenticationException::class,
LaravelAuthorizationException::class,
ModelNotFoundException::class,
TokenMismatchException::class,
\Illuminate\Validation\ValidationException::class,
\Symfony\Component\HttpKernel\Exception\HttpException::class,
// local
AuthorizationException::class,
SilencedException::class,
// oauth
\League\OAuth2\Server\Exception\OAuthServerException::class,
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
*
* @return void
*/
public function report(Exception $e)
{
view()->share('current_action', $this->statusCode($e));
view()->share('current_section', 'error');
// immediately done if the error should not be reported
if ($this->shouldntReport($e)) {
return;
}
if (config('sentry.dsn')) {
$this->reportWithSentry($e);
}
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
*
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if (method_exists($e, 'getResponse')) {
return $e->getResponse();
}
if ($e instanceof AuthenticationException) {
return $this->unauthenticated($request, $e);
}
if (config('app.debug')) {
if ($this->isHttpException($e)) {
$response = $this->renderHttpException($e);
} else {
$response = parent::render($request, $e);
}
} else {
$message = $this->exceptionMessage($e);
if ($request->expectsJson()) {
$response = response(['error' => $message]);
} else {
$response = response()->view('layout.error', ['exceptionMessage' => $message]);
}
}
return $response->setStatusCode($this->statusCode($e));
}
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response(['authentication' => 'basic'], 401);
}
return response()->view('users.login');
}
private function exceptionMessage($e)
{
if ($e instanceof ModelNotFoundException) {
return;
}
if ($this->statusCode($e) >= 500) {
return;
}
return $e->getMessage();
}
private function reportWithSentry($e)
{
$extra = [
'http_code' => $this->statusCode($e),
];
if (Auth::check()) {
$userContext = [
'id' => Auth::user()->user_id,
'username' => Auth::user()->username_clean,
];
} else {
$userContext = [
'id' => null,
];
}
Sentry::user_context($userContext);
$ref = Sentry::captureException($e, compact('extra'));
view()->share('ref', $ref);
}
private function statusCode($e)
{
if (method_exists($e, 'getStatusCode')) {
return $e->getStatusCode();
} elseif ($e instanceof ModelNotFoundException) {
return 404;
} elseif ($e instanceof NotFoundHttpException) {
return 404;
} elseif ($e instanceof TokenMismatchException) {
return 403;
} elseif ($e instanceof AuthenticationException) {
return 401;
} elseif ($e instanceof AuthorizationException) {
return 403;
} else {
return 500;
}
}
}