Skip to content

Commit 40d38e3

Browse files
authored
Merge pull request ppy#11069 from nanaya/sentry-cleanup
Clean up sentry reporting
2 parents afea9a3 + adbf6a3 commit 40d38e3

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

app/Exceptions/Handler.php

+11-26
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace App\Exceptions;
77

88
use App\Libraries\SessionVerification;
9-
use Auth;
109
use Illuminate\Auth\Access\AuthorizationException as LaravelAuthorizationException;
1110
use Illuminate\Auth\AuthenticationException;
1211
use Illuminate\Database\Eloquent\ModelNotFoundException;
@@ -34,6 +33,7 @@ class Handler extends ExceptionHandler
3433
ModelNotFoundException::class,
3534
TokenMismatchException::class,
3635
\Illuminate\Validation\ValidationException::class,
36+
\Laravel\Octane\Exceptions\DdException::class,
3737
\Symfony\Component\HttpKernel\Exception\HttpException::class,
3838

3939
// local
@@ -90,6 +90,15 @@ private static function isOAuthSessionException(Throwable $e): bool
9090
&& $e->getMessage() === 'Authorization request was not present in the session.';
9191
}
9292

93+
private static function reportWithSentry(Throwable $e): void
94+
{
95+
$ref = log_error_sentry($e, ['http_code' => (string) static::statusCode($e)]);
96+
97+
if ($ref !== null) {
98+
\Request::instance()->attributes->set('ref', $ref);
99+
}
100+
}
101+
93102
private static function unwrapViewException(Throwable $e): Throwable
94103
{
95104
if ($e instanceof ViewException) {
@@ -121,10 +130,7 @@ public function report(Throwable $e)
121130
return;
122131
}
123132

124-
// Fallback in case error happening before config is initialised
125-
if ($GLOBALS['cfg']['sentry']['dsn'] ?? false) {
126-
$this->reportWithSentry($e);
127-
}
133+
static::reportWithSentry($e);
128134

129135
parent::report($e);
130136
}
@@ -196,25 +202,4 @@ protected function unauthenticated($request, AuthenticationException $exception)
196202

197203
return ext_view('users.login', null, null, 401);
198204
}
199-
200-
private function reportWithSentry($e)
201-
{
202-
if (Auth::check()) {
203-
$userContext = [
204-
'id' => Auth::user()->user_id,
205-
'username' => Auth::user()->username_clean,
206-
];
207-
} else {
208-
$userContext = [
209-
'id' => null,
210-
];
211-
}
212-
213-
app('sentry')->configureScope(function ($scope) use ($e, $userContext) {
214-
$scope->setUser($userContext);
215-
$scope->setTag('http_code', (string) static::statusCode($e));
216-
});
217-
218-
request()->attributes->set('ref', app('sentry')->captureException($e));
219-
}
220205
}

app/helpers.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,35 @@ function json_time(?DateTime $time): ?string
468468
return $time === null ? null : $time->format(DateTime::ATOM);
469469
}
470470

471-
function log_error($exception)
471+
function log_error($exception, ?array $sentryTags = null): void
472472
{
473473
Log::error($exception);
474+
log_error_sentry($exception, $sentryTags);
475+
}
474476

475-
if ($GLOBALS['cfg']['sentry']['dsn']) {
476-
Sentry::captureException($exception);
477+
function log_error_sentry(Throwable $exception, ?array $tags = null): ?string
478+
{
479+
// Fallback in case error happening before config is initialised
480+
if (!($GLOBALS['cfg']['sentry']['dsn'] ?? false)) {
481+
return null;
477482
}
483+
484+
return Sentry\withScope(function ($scope) use ($exception, $tags) {
485+
$currentUser = Auth::user();
486+
$userContext = $currentUser === null
487+
? ['id' => null]
488+
: [
489+
'id' => $currentUser->getKey(),
490+
'username' => $currentUser->username,
491+
];
492+
493+
$scope->setUser($userContext);
494+
foreach ($tags ?? [] as $key => $value) {
495+
$scope->setTag($key, $value);
496+
}
497+
498+
return Sentry\captureException($exception);
499+
});
478500
}
479501

480502
function logout()

0 commit comments

Comments
 (0)