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
30
31
32
33
34
35
36
# File 'lib/orange-core/middleware/show_exceptions.rb', line 20

def call(env)
  @app.call(env)
rescue Exception => e
  if(orange.options[:development_mode])
    backtrace = pretty(env, e)
  
    return [500,
     {"Content-Type" => "text/html",
      "Content-Length" => backtrace.join.size.to_s},
     backtrace]
  else
    page = error_page(env)
    return [500, {"Content-Type" => "text/html",
      "Content-Length" => page.join.size.to_s},
     page]
  end
end

#error_page(env) ⇒ Object



42
43
44
45
46
# File 'lib/orange-core/middleware/show_exceptions.rb', line 42

def error_page(env)
  packet = Orange::Packet.new(@core, env)
  parse = orange[:parser].haml("500.haml", packet, :template => true)
  [parse]
end

#h(obj) ⇒ Object

:nodoc:



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/orange-core/middleware/show_exceptions.rb', line 84

def h(obj)                  # :nodoc:
  case obj
  when String
    Rack::Utils.escape_html(obj)
  else
    begin 
      Rack::Utils.escape_html(obj.inspect)
    rescue Exception => e
      "Object #{obj.class.to_s} could not be inspected"
    end
  end
end

#packet_call(packet) ⇒ Object



38
39
40
# File 'lib/orange-core/middleware/show_exceptions.rb', line 38

def packet_call(packet)
  backtrace = pretty()
end

#pretty(env, exception) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/orange-core/middleware/show_exceptions.rb', line 48

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