Class: Orange::Middleware::ShowExceptions

Inherits:
Base show all
Defined in:
lib/orange-core/middleware/show_exceptions.rb

Overview

Rack::ShowExceptions catches all exceptions raised from the app it wraps. It shows a useful backtrace with the sourcefile and clickable context, the whole Rack environment and the request data.

Be careful when you use this on public-facing sites as it could reveal information helpful to attackers.

Orange::Middleware::ShowExceptions is a slightly modified version of Rack::ShowExceptions

Constant Summary collapse

CONTEXT =
7

Instance Method Summary collapse

Methods inherited from Base

#init, #initialize, #inspect, #orange, #pass, #recapture

Constructor Details

This class inherits a constructor from Orange::Middleware::Base

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/orange-core/middleware/show_exceptions.rb', line 20

def call(env)
  @app.call(env)
rescue Exception => e
  backtrace = pretty(env, e)
  
  [500,
   {"Content-Type" => "text/html",
    "Content-Length" => backtrace.join.size.to_s},
   backtrace]
end

#h(obj) ⇒ Object

:nodoc:



71
72
73
74
75
76
77
78
# File 'lib/orange-core/middleware/show_exceptions.rb', line 71

def h(obj)                  # :nodoc:
  case obj
  when String
    Rack::Utils.escape_html(obj)
  else
    Rack::Utils.escape_html(obj.inspect)
  end
end

#packet_call(packet) ⇒ Object



31
32
33
# File 'lib/orange-core/middleware/show_exceptions.rb', line 31

def packet_call(packet)
  backtrace = pretty()
end

#pretty(env, exception) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/orange-core/middleware/show_exceptions.rb', line 35

def pretty(env, exception)
  req = Rack::Request.new(env)
  path = (req.script_name + req.path_info).squeeze("/")

  frames = exception.backtrace.map { |line|
    frame = OpenStruct.new
    if line =~ /(.*?):(\d+)(:in `(.*)')?/
      frame.filename = $1
      frame.lineno = $2.to_i
      frame.function = $4

      begin
        lineno = frame.lineno-1
        lines = ::File.readlines(frame.filename)
        frame.pre_context_lineno = [lineno-CONTEXT, 0].max
        frame.pre_context = lines[frame.pre_context_lineno...lineno]
        frame.context_line = lines[lineno].chomp
        frame.post_context_lineno = [lineno+CONTEXT, lines.size].min
        frame.post_context = lines[lineno+1..frame.post_context_lineno]
      rescue
      end

      frame
    else
      nil
    end
  }.compact

  env["rack.errors"].puts "#{exception.class}: #{exception.message}"
  env["rack.errors"].puts exception.backtrace.map { |l| "\t" + l }
  env["rack.errors"].flush
  orange_env = env["orange.env"]
  parse = orange[:parser].haml("exceptions.haml", binding, :template => true)
  [parse]
end