Hi,
I am using Rails 6 with Session::CookieStore. I noticed that, whenever something goes wrong during session state loading (e.g., data in the cookie cannot be parsed because storage mode has been switched from Marshal to JSON), Rails raises a hard error (e.g., JSON::ParserError
) and fails the request.
Is this expected behaviour? It seems far from optimal to me. Instead, Rails should fail gracefully by re-setting the session if loading fails.
I can implement graceful recovery in the following way:
before_action :load_session_with_fallback
def load_session_with_fallback
# trigger session loading attempt
session[:foo]
rescue => e
logger.error "Session loading failed: " + e.inspect
#reset_session does not work here! need to manually re-set the session cookie
cookies[:_forum_session] = nil
if @retried
raise e
else
@retried = true
retry
end
end
But it seems a bit hacky to re-set the cookie manually (reset_session does not have the intended effect).
Any better suggestion for how to implement graceful recovery for failed session loads? Should this be a standard rails feature?
Thanks, Andreas