Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear error page state before each request #448

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/better_errors/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,24 @@ def better_errors_call(env)
end

def protected_app_call(env)
# Hold any existing error outside the instance that it's not going to pollute the `#inspect` of this instance
# while the app is being called. Once the app returns, restore the original error so that it is still available
# to Better Errors. If a new error is raised during the call, it be stored replacing the old one.
#
# FIXME: wrap this (and therefore all application requests) in a mutex.
# This will have strange results in a multithreaded environment. Either the second thread will clear
# the error state, or another thread request that rescues an exception will be overwritten by a slower
# request finishing after it.
Thread.current[:better_errors_previous_error_page] = @error_page
@error_page = nil
@app.call env
@error_page = Thread.current[:better_errors_previous_error_page]
rescue Exception => ex
@error_page = @handler.new ex, env
log_exception
show_error_page(env, ex)
ensure
Thread.current[:better_errors_previous_error_page] = nil
end

def show_error_page(env, exception=nil)
Expand Down