Class: Gallerist::ShowExceptions

Inherits:
Sinatra::ShowExceptions
  • Object
show all
Defined in:
lib/gallerist/middleware/show_exceptions.rb

Overview

Monkey-patch for Sinatra 1.4 and Rack 1.6

See github.com/sinatra/sinatra/issues/951

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gallerist/middleware/show_exceptions.rb', line 11

def call(env)
  @app.call(env)
rescue Exception => e
  raise if env['rack.warmup']

  errors, env["rack.errors"] = env["rack.errors"], @@eats_errors

  if prefers_plain_text?(env)
    content_type = "text/plain"
    exception_string = dump_exception(e)
  else
    content_type = "text/html"
    exception_string = pretty(env, e)
  end

  env["rack.errors"] = errors

  # Post 893a2c50 in rack/rack, the #pretty method above, implemented in
  # Rack::ShowExceptions, returns a String instead of an array.
  body = Array(exception_string)

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