Skip to content

Commit

Permalink
Work around weirdness in older Rack::Response
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDaugherty committed Sep 13, 2020
1 parent 764bf3b commit ef06461
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/better_errors/middleware.rb
Expand Up @@ -110,16 +110,17 @@ def show_error_page(env, exception=nil)
status_code = ActionDispatch::ExceptionWrapper.new(env, exception).status_code
end

headers = {
"Content-Type" => "text/#{type}; charset=utf-8",
}
response = Rack::Response.new(content, status_code, headers)
response = Rack::Response.new(content, status_code, { "Content-Type" => "text/#{type}; charset=utf-8" })

unless request.cookies[CSRF_TOKEN_COOKIE_NAME]
response.set_cookie(CSRF_TOKEN_COOKIE_NAME, value: csrf_token, httponly: true, same_site: :strict)
end

response.finish
# In older versions of Rack, the body returned here is actually a Rack::BodyProxy which seems to be a bug.
# (It contains status, headers and body and does not act like an array of strings.)
# Since we already have status code and body here, there's no need to use the ones in the Rack::Response.
(_status_code, headers, _body) = response.finish
[status_code, headers, [content]]
end

def text?(env)
Expand Down

0 comments on commit ef06461

Please sign in to comment.