Class: Low::Rack::Exceptions

Inherits:
Object
  • Object
show all
Defined in:
lib/low/rack/exceptions.rb

Overview

Low::Rack::Exceptions is like Rack::ShowExceptions, except it will only actually respond with the stack trace when give permission to. Also, it don’t do HTML.

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Exceptions

Returns a new instance of Exceptions.



11
12
13
14
# File 'lib/low/rack/exceptions.rb', line 11

def initialize(app, opts = {})
  @app = app
  @logger_key = opts[:logger_key] || Low::Rack::Default::LOGGER_KEY
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/low/rack/exceptions.rb', line 16

def call(env)
  @app.call(env)
rescue => exception

  # Format the exception trace
  trace = exception.message + "\n" + exception.backtrace.join("\n")

  # and log it, if we have a logger, or raw IO.
  if logger = env[@logger_key]
    logger.fatal trace
  elsif io = env['rack.errors']
    io.puts(trace)
    io.flush
  end

  # Respond with the trace if appropriate.
  if env['low.show_exceptions']
    message = trace
  else
    message = 'An internal server error occurred.'
  end

  [500, {'Content-Type' => 'text/plain'}, [message]]
end