In my Laravel 5.2 project, I have a middleware happily storing requests and responses to DB or files.
There I serialize/json_encode $request object for logging everything going on. (cookies, input, files, headers...)
I need to create an error handler that will use whole request object to include everything about request into the report email. But ExceptionHandler::report() does not accept Request as a parameter.
Laravel 5.2 provides the helper method request(), which works for this use case:
/**
 * Report or log an exception.
 *
 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
 *
 * @param  \Exception  $exception
 * @return void
 */
public function report(Exception $exception)
{
    $request = request();
    parent::report($exception);
}
                        In App\Exceptions\Handler.php and the render method wich does have the request as parameter. here you could fire an event to store the stuff in a session or database.
For example:
public function render($request, Exception $e)
    {
        if ($e instanceof HttpException) {
            if ($e->getStatusCode() == 403) {
                Event::fire(new UserNotAllowed($request));
                return redirect()->to("/home");
            }
            if ($e->getStatusCode() == 404) {
                if (Auth::guest()) {
                    return redirect()->to("/");
                }
            }
        }
        if ($e instanceof ModelNotFoundException) {          
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }
        return parent::render($request, $e);
    }
more info here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With