Skip to content

Commit

Permalink
Merge pull request #109 from gsamokovarov/revamp-error-messages
Browse files Browse the repository at this point in the history
Revamp unavailable session response message
  • Loading branch information
gsamokovarov committed Feb 24, 2015
2 parents 98a203a + 08f51a5 commit 3775fe0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 13 additions & 4 deletions lib/web_console/middleware.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'active_support/core_ext/string/strip'

module WebConsole
class Middleware
TEMPLATES_PATH = File.expand_path('../templates', __FILE__)
Expand All @@ -7,6 +9,13 @@ class Middleware
binding_change_re: %r{/repl_sessions/(?<id>.+?)/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

Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/web_console/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3775fe0

Please sign in to comment.