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

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

Constant Summary

Constants included from Action

Action::RENDER

Instance Method Summary collapse

Methods included from Action

included

Instance Method Details

#backtrace(env) ⇒ Object



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
# File 'lib/rackr/router/errors/dev_html.rb', line 50

def backtrace(env)
  first, *tail = env['error'].backtrace
  h2 do
    _ 'Traceback '
    span '(innermost first)'
  end

  tag :p, first, class: '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.open(file_path) do |file|
      lines = file.readlines
    end

    lines.map!.with_index do |line, i|
      "#{i + 1}: #{line} \n"
    end

    tag :pre, slice_around_index(lines, line_number).join('').chomp
  end

  tag :p, tail.join("\n")
end

#call(env) ⇒ Object



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
# File 'lib/rackr/router/errors/dev_html.rb', line 12

def call(env)
  html_layout do
    tag :head do
      title 'Application error'
      _ '<style>
            html * { padding:0; margin:0; }
            body * { padding:10px 20px; }
            body * * { padding:0; }
            body { font:small sans-serif; }
            body>div { border-bottom:1px solid #ddd; }
            h1 { font-weight:normal; }
            h2 { margin-bottom:.8em; }
            h2 span { font-size:80%; color:#666; font-weight:normal; }
            #summary { background: #ffc; }
            #summary h2 { font-weight: normal; color: #666; }
            pre {
              background: #f8f8f8;
              padding: 1em;
              margin-bottom: 1em;
            }
      </style>'
    end
    tag :body do
      div id: 'summary' do
        h1 env['error'].class.to_s
        if env['error'].message.size > 1000
          h2 "#{env['error'].message.slice(0, 1000)} ..."
        else
          h2 env['error'].message
        end
      end
      div id: 'backtrace' do
        backtrace(env)
      end
    end
  end
end

#extract_line_number(input) ⇒ Object



79
80
81
82
83
# File 'lib/rackr/router/errors/dev_html.rb', line 79

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

#slice_around_index(array, index) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/rackr/router/errors/dev_html.rb', line 85

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

  # Slice the array
  array[start_index..end_index]
end