Class: Rackr::Router::Errors::DevHtml

Inherits:
Object
  • Object
show all
Includes:
Action
Defined in:
lib/rackr/router/errors/dev_html.rb

Instance Method Summary collapse

Methods included from Action

included

Instance Method Details

#backtrace(env) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rackr/router/errors/dev_html.rb', line 51

def backtrace(env)
  first, *tail = env['error'].backtrace
  traceback = String.new('<h2>Traceback <span>(innermost first)</span></h2>')
  traceback << "<p class=\"first-p\">#{first}</p><br/>"

  line_number = extract_line_number(first)
  match = first.match(%r{^(/[[\w/.-]]+)})
  file_path = (match ? match[1] : nil)
  unless file_path.nil?
    lines = File.readlines(file_path).map.with_index { |line, i| "#{i + 1}: #{line}" }
    traceback << "<pre>#{slice_around_index(lines, line_number).join('')}</pre>"
  end

  traceback << "<p>#{tail.join('<br>')}</p>"
  traceback
end

#call(env) ⇒ Object



9
10
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
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rackr/router/errors/dev_html.rb', line 9

def call(env)
  render(html: "    <!DOCTYPE html>\n    <html>\n    <head>\n      <title>Application error</title>\n      <style>\n        html * { padding:0; margin:0; }\n        body * { padding:10px 20px; }\n        body * * { padding:0; }\n        body { font:small sans-serif; }\n        body>div { border-bottom:1px solid #ddd; }\n        h1 { font-weight:normal; }\n        h2 { margin-bottom:.8em; }\n        h2 span { font-size:80%; color:#666; font-weight:normal; }\n        #summary { background: #ffc; }\n        #summary h2 { font-weight: normal; color: #666; }\n        #backtrace { background: #eee; }\n        pre {\n          background: #ddd;\n          padding: 1em;\n          margin-bottom: 1em;\n        }\n        p {\n          font-family: monospace;\n        }\n      </style>\n    </head>\n    <body>\n      <div id=\"summary\">\n        <h1>\#{env['error'].class}</h1>\n        <h2>\#{env['error'].message.size > 1000 ? \"\#{env['error'].message.slice(0, 1000)} ...\" : env['error'].message}</h2>\n      </div>\n      <div id=\"backtrace\">\n        \#{backtrace(env)}\n      </div>\n    </body>\n    </html>\n  HTML\n        )\nend\n"

#extract_line_number(input) ⇒ Object



68
69
70
71
72
# File 'lib/rackr/router/errors/dev_html.rb', line 68

def extract_line_number(input)
  if (match = input.match(/:(\d+):in/))
    match[1].to_i
  end
end

#slice_around_index(array, index) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/rackr/router/errors/dev_html.rb', line 74

def slice_around_index(array, index)
  return array if index.nil? || index < 1

  index -= 1
  start_index = [index - 2, 0].max
  end_index = [index + 2, array.size - 1].min

  array[start_index..end_index]
end