Skip to content

Commit

Permalink
feat: support sentry reporting (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Dec 13, 2020
1 parent 04f63df commit 16ec676
Show file tree
Hide file tree
Showing 10 changed files with 1,442 additions and 291 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ jobs:
run: |
cp tests/.env.ci-${{ matrix.connection }} .env
touch config/version
touch config/release
echo '{"/js/app.js": "/js/app.js", "/css/app.css": "/css/app.css"}' > public/mix-manifest.json
mkdir -p public/js
echo '' > public/js/app.js
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ jobs:
run: |
cp tests/.env.ci-cypress .env
touch config/version
touch config/release
- name: Create SQLite database
run: |
mkdir -p database
Expand Down
26 changes: 25 additions & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Throwable;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
Expand All @@ -15,7 +19,10 @@ class Handler extends ExceptionHandler
* @var array
*/
protected $dontReport = [
//
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
];

/**
Expand Down Expand Up @@ -46,4 +53,21 @@ public function render($request, Throwable $e)

return parent::render($request, $e);
}

/**
* Report or log an exception.
*
* @param \Throwable $e
* @return void
*
* @throws \Throwable
*/
public function report(Throwable $e)
{
if (config('app.sentry.enable') && app()->bound('sentry') && $this->shouldReport($e)) {
app('sentry')->captureException($e); // @codeCoverageIgnore
}

parent::report($e);
}
}
4 changes: 3 additions & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
'sentry.context',
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
Expand All @@ -41,6 +41,7 @@ class Kernel extends HttpKernel

'api' => [
'throttle:api',
'sentry.context',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
Expand All @@ -59,6 +60,7 @@ class Kernel extends HttpKernel
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'sentry.context' => \App\Http\Middleware\SentryContext::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \App\Http\Middleware\EnsureEmailIsVerified::class,
Expand Down
40 changes: 40 additions & 0 deletions app/Http/Middleware/SentryContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Sentry\State\Scope;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

class SentryContext
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*
* @codeCoverageIgnore
*/
public function handle($request, Closure $next)
{
if (app()->bound('sentry') && config('app.sentry.enable')) {
\Sentry\configureScope(function (Scope $scope): void {
// Add user context
$user = Auth::user();
if ($user !== null) {
$scope->setUser([
'id' => $user->id,
]);
}
$scope->setTag('page.route.name', Route::currentRouteName());
$scope->setTag('page.route.action', Route::currentRouteAction());
});
}

return $next($request);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"mariuzzo/laravel-js-localization": "^1.7",
"moneyphp/money": "^3.3",
"parsedown/laravel": "^1.2",
"sentry/sentry-laravel": "^2.3",
"spatie/laravel-activitylog": "^3.1",
"tightenco/ziggy": "^1.0"
},
Expand Down

0 comments on commit 16ec676

Please sign in to comment.