Untitled
unknown
plain_text
a year ago
2.9 kB
15
Indexable
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Validation\ValidationException;
use Sentry\State\Scope;
use App\Utils\Errors;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
public function render($request, Throwable $exception)
{
if ($exception instanceof ModelNotFoundException) {
return Errors::noDataFound();
}
if ($exception instanceof AuthorizationException) {
return Errors::unauthorizedAccess($exception->getMessage());
}
return parent::render($request, $exception);
}
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Throwable $e) {
if (app()->bound('sentry')) {
app('sentry')->configureScope(function (Scope $scope) {
if (request()) {
// Capture request context
$request = request();
$scope->setContext('Request Info', [
'url' => $request->fullUrl(),
'method' => $request->method(),
'ip' => $request->ip(),
'user_agent' => $request->header('User-Agent'),
'headers' => $request->headers->all(),
'query_parameters' => $request->query(),
'body' => $request->all(), // Be cautious with logging sensitive data
]);
}
// Capture user context
if (auth()->check()) {
$scope->setUser([
'id' => auth()->id(),
'email' => auth()->user()->email,
]);
}
});
// Capture exception in Sentry
app('sentry')->captureException($e);
}
});
}
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->is('api/*')) {
return Errors::unauthorized();
}
return redirect()->guest(route('login'));
}
}
Editor is loading...
Leave a Comment