Skip to content

Commit

Permalink
add user info and possibly error form to sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
tbirrell committed Aug 19, 2020
1 parent 7b189c0 commit 7c3fc60
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 12 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ SESSION_DOMAIN=realitystockwatch.com
FEEDBACK_URL=

FATHOM_SITE_ID=
SENTRY_LARAVEL_DSN=null

HASH_SALT=

Expand Down
21 changes: 15 additions & 6 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Exceptions;

use Symfony\Component\HttpFoundation\Response;
use Throwable;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\HttpException;

class Handler extends ExceptionHandler
{
Expand All @@ -29,7 +31,7 @@ class Handler extends ExceptionHandler
/**
* Report or log an exception.
*
* @param \Exception $exception
* @param Throwable $exception
* @return void
*
* @throws \Exception
Expand All @@ -41,20 +43,27 @@ public function report(Throwable $exception)
}

parent::report($exception);

}

/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Symfony\Component\HttpFoundation\Response
* @param \Illuminate\Http\Request $request
* @param Throwable $exception
* @return Response
*
* @throws \Exception
* @throws Throwable
*/
public function render($request, Throwable $exception)
{
// Convert all non-http exceptions to a proper 500 http exception
// if we don't do this exceptions are shown as a default template
// instead of our own view in resources/views/errors/500.blade.php
if ($this->shouldReport($exception) && !$this->isHttpException($exception) && !config('app.debug')) {
$exception = new HttpException(500, 'Whoops!');
}

return parent::render($request, $exception);
}
}
32 changes: 32 additions & 0 deletions app/Http/Middleware/SentryContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Sentry\State\Scope;

class SentryContext
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, Closure $next)
{
if (auth()->check() && app()->bound('sentry')) {
\Sentry\configureScope(function (Scope $scope): void {
$scope->setUser([
'id' => auth()->user()->id,
'name' => auth()->user()->name,
'email' => auth()->user()->email,
]);
});
}

return $next($request);
}
}
2 changes: 1 addition & 1 deletion config/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
],

// @see: https://docs.sentry.io/error-reporting/configuration/?platform=php#send-default-pii
'send_default_pii' => false,
'send_default_pii' => true,

];
15 changes: 14 additions & 1 deletion resources/views/errors/500.blade.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
@extends('errors::minimal')
@extends('errors::illustrated-layout')

@section('title', __('Server Error'))
@section('code', '500')
@section('message')
@if(Session::has('error'))
{{Session::get('error') }}
@else
Something went wrong.
@endif
@endsection
@section('image')
<div class="content">
@if(app()->bound('sentry') && app('sentry')->getLastEventId())
<div class="subtitle">Error ID: {{ app('sentry')->getLastEventId() }}</div>
<script>
Sentry.init({ dsn: {{ env('SENTRY_LARAVEL_DSN') }} });
Sentry.showReportDialog({ eventId: '{{ app('sentry')->getLastEventId() }}' });
</script>
@endif
</div>
@endsection
7 changes: 3 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@
dump($m->hashid);
}
});
Route::get('/test/error/{error}', function ($error) {
abort($error);
});

});

Route::get('/profile', [ProfileController::class, 'index'])->name('profile');

Route::get('/debug-sentry', function () {
throw new Exception('My first Sentry error!');
});


//===

Expand Down

0 comments on commit 7c3fc60

Please sign in to comment.