Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
ShowExceptions minor refactoring
* Load HTML exception template only if needed
* Only #call is public
* Enumerable body concern in one place
  • Loading branch information
bestie authored and raggi committed Jul 13, 2014
1 parent 2ab24cf commit 893a2c5
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/rack/showexceptions.rb
Expand Up @@ -17,7 +17,6 @@ class ShowExceptions

def initialize(app)
@app = app
@template = ERB.new(TEMPLATE)
end

def call(env)
Expand All @@ -33,15 +32,21 @@ def call(env)
body = pretty(env, e)
else
content_type = "text/plain"
body = [exception_string]
body = exception_string
end

[500,
{"Content-Type" => content_type,
"Content-Length" => Rack::Utils.bytesize(body.join).to_s},
body]
[
500,
{
"Content-Type" => content_type,
"Content-Length" => Rack::Utils.bytesize(body).to_s,
},
[body],
]
end

private

def accepts_html?(env)
env["HTTP_ACCEPT"] && env["HTTP_ACCEPT"].include?("text/html")
end
Expand Down Expand Up @@ -85,7 +90,7 @@ def pretty(env, exception)
end
}.compact

[@template.result(binding)]
ERB.new(TEMPLATE).result(binding)
end

def h(obj) # :nodoc:
Expand Down

2 comments on commit 893a2c5

@vipulnsward
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change in return type causes sinatra/sinatra#906 . Is this method public?

@raggi
Copy link
Member

@raggi raggi commented on 893a2c5 Jul 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the method is not public.

Please sign in to comment.