From 08f51a5975641879dd852988aa564352c769adc3 Mon Sep 17 00:00:00 2001 From: Genadi Samokovarov Date: Sat, 21 Feb 2015 16:11:35 +0200 Subject: [PATCH] Revamp unavailable session response message When a user hit this message, it usually means two things: 1. The underlying server have been restarted. 2. The user runs in multi-process server and the request hit a worker, which doesn't store the session in memory. I'm trying to hit the users here about the second case, since we received yet another report of this happening and people getting confused of why the console doesn't work. --- lib/web_console/middleware.rb | 17 +++++++++++++---- test/web_console/middleware_test.rb | 4 ++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/web_console/middleware.rb b/lib/web_console/middleware.rb index 3fc539fd..44b687fb 100644 --- a/lib/web_console/middleware.rb +++ b/lib/web_console/middleware.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/string/strip' + module WebConsole class Middleware TEMPLATES_PATH = File.expand_path('../templates', __FILE__) @@ -7,6 +9,13 @@ class Middleware binding_change_re: %r{/repl_sessions/(?.+?)/trace\z} } + UNAVAILABLE_SESSION_MESSAGE = <<-END.strip_heredoc + Session %{id} is is no longer available in memory. + + If you happen to run on a multi-process server (like Unicorn) the process + this request hit doesn't store %{id} in memory. + END + cattr_accessor :whiny_requests @@whiny_requests = true @@ -73,7 +82,7 @@ def id_for_repl_session_stack_frame_change(request) def update_repl_session(id, params) unless session = Session.find(id) - return respond_with_unavailable_session + return respond_with_unavailable_session(id) end status = 200 @@ -85,7 +94,7 @@ def update_repl_session(id, params) def change_stack_trace(id, params) unless session = Session.find(id) - return respond_with_unavailable_session + return respond_with_unavailable_session(id) end session.switch_binding_to(params[:frame_id]) @@ -97,10 +106,10 @@ def change_stack_trace(id, params) Rack::Response.new(body, status, headers).finish end - def respond_with_unavailable_session + def respond_with_unavailable_session(id) status = 404 headers = { 'Content-Type' => 'application/json; charset = utf-8' } - body = { output: 'Unavailable session' }.to_json + body = { output: format(UNAVAILABLE_SESSION_MESSAGE, id: id)}.to_json Rack::Response.new(body, status, headers).finish end diff --git a/test/web_console/middleware_test.rb b/test/web_console/middleware_test.rb index 422099b3..baf041f2 100644 --- a/test/web_console/middleware_test.rb +++ b/test/web_console/middleware_test.rb @@ -96,13 +96,13 @@ def call(env) test 'unavailable sessions respond to the user with a message' do xhr :put, '/repl_sessions/no_such_session', { input: '__LINE__' } - assert_equal({ output: 'Unavailable session' }.to_json, response.body) + assert_equal(404, response.status) end test 'unavailable sessions can occur on binding switch' do xhr :post, "/repl_sessions/no_such_session/trace", { frame_id: 1 } - assert_equal({ output: 'Unavailable session' }.to_json, response.body) + assert_equal(404, response.status) end private