TokenMismatchException in handler, sometimes ignored

Posted 2 years ago by francois

Hi folks,

I have a very strange behavior in my app. I've added a custom way to respond on a TokenMismatchException. This works most of the time, but sometimes users have the default woops page for all 500 errors.

From what I can see, it's only on mobile (98% on IPhone). I know it's not related, but I just don't understand how the error can be reported, becausee it has to ba catched by the Handler.

Here is the handler code :

/**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if ($e instanceof TokenMismatchException) {
            if ($request->expectsJson()) {
                return new JsonResponse(['session' => 'custom message'], 422);
            }
            return back()->withErrors(['session' => 'custom message']);

        }

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

And a complet stacktrace :

[2017-04-01 09:47:41] production.ERROR: exception 'Illuminate\Session\TokenMismatchException' in /my_dir/bootstrap/cache/compiled.php:3314
Stack trace:
#0 /my_dir/bootstrap/cache/compiled.php(10069): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#1 /my_dir/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#2 /my_dir/bootstrap/cache/compiled.php(13749): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#3 /my_dir/bootstrap/cache/compiled.php(10069): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#4 /my_dir/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#5 /my_dir/bootstrap/cache/compiled.php(12155): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#6 /my_dir/bootstrap/cache/compiled.php(10069): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#7 /my_dir/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#8 /my_dir/bootstrap/cache/compiled.php(13495): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#9 /my_dir/bootstrap/cache/compiled.php(10069): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 /my_dir/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 /my_dir/bootstrap/cache/compiled.php(13432): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#12 /my_dir/bootstrap/cache/compiled.php(10069): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#13 /my_dir/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#14 /my_dir/bootstrap/cache/compiled.php(10054): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#15 /my_dir/bootstrap/cache/compiled.php(8356): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#16 /my_dir/bootstrap/cache/compiled.php(8347): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#17 /my_dir/bootstrap/cache/compiled.php(8338): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#18 /my_dir/bootstrap/cache/compiled.php(2575): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#19 /my_dir/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#20 /my_dir/vendor/rikless/laravel-caffeine/src/Http/Middleware/LaravelCaffeineDripMiddleware.php(17): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#21 /my_dir/bootstrap/cache/compiled.php(10069): GeneaLabs\LaravelCaffeine\Http\Middleware\LaravelCaffeineDripMiddleware->handle(Object(Illuminate\Http\Request), Object(Closure))
#22 /my_dir/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#23 /my_dir/app/Http/Middleware/CheckForMaintenanceMode.php(43): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#24 /my_dir/bootstrap/cache/compiled.php(10069): App\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#25 /my_dir/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#26 /my_dir/bootstrap/cache/compiled.php(10054): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#27 /my_dir/bootstrap/cache/compiled.php(2519): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#28 /my_dir/bootstrap/cache/compiled.php(2503): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#29 /my_dir/public/index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#30 {main}

Please sign in or create an account to participate in this conversation.