Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ActionDispatch::Session::SessionRestoreError be rescued?

Occasionally breaking changes cause ActionDispatch::Session::SessionRestoreError exceptions. It would be great to be able to do something like this to automatically clear invalid sessions:

class ApplicationController < ActionController::Base
  rescue_from ActionDispatch::Session::SessionRestoreError do |exception|
    reset_session
    redirect_to :home
  end
end

This doesn't work -- I'm assuming because the exception is happening at the lower ActionDispatch layer. Is there a way to recover from these errors?

like image 677
Kris Braun Avatar asked Aug 06 '12 19:08

Kris Braun


1 Answers

It can. The robust solution is following:

  1. Open config/initializers/secret_token.rb
  2. Change the value for YourApp::Application.config.secret_token, e.g. by replacing the last char to something else
  3. Restart the Rails server

I know it doesn't directly solve the problem in question and automating it is relatively hard and not a very good idea (autorewriting app config), but it could be an (expensive) option.

I experienced it with large chunks being written to a session so you can also make sure to check your session against any extensive data storage and swop it to the database.

like image 149
Ain Tohvri Avatar answered Nov 07 '22 20:11

Ain Tohvri