module Roda::RodaPlugins::ExceptionPage::InstanceMethods

  1. lib/roda/plugins/exception_page.rb

Public Instance methods

exception_page(exception, opts=(no_positional_opts = true; OPTS), assets: opts[:assets], context: opts[:context], css_file: opts[:css_file], js_file: opts[:js_file], json: opts[:json], **kw)

Return a HTML page showing the exception, allowing a developer more information for debugging. Designed to be called inside an exception handler, passing in the received exception. Sets the Content-Type header in the response, and returns the string used for the body. If the Accept request header is present and text/html is accepted, return an HTML page with the backtrace with the ability to see the context for each backtrace line, as well as the GET, POST, cookie, and rack environment data. If text/html is not accepted, then just show a plain text page with the exception class, message, and backtrace.

Arguments:

assets

If true, sets :css_file to /exception_page.css and :js_file to /exception_page.js, assuming that r.exception_page_assets is called in the route block to serve the exception page assets. If a String, uses the string as a prefix, assuming that r.exception_page_assets is called in a nested block inside the route block. If false, doesn’t use any CSS or JS.

context

The number of context lines before and after each line in the backtrace (default: 7).

css_file

A path to the external CSS file for the HTML exception page. If false, doesn’t use any CSS.

js_file

A path to the external javascript file for the HTML exception page. If false, doesn’t use any JS.

json

Return a hash of exception information. The hash will have a single key, "exception", with a value being a hash with three keys, "class", "message", and "backtrace", which contain information derived from the given exception. Designed to be used with the json plugin, which will automatically convert the hash to JSON format.

[show source]
    # File lib/roda/plugins/exception_page.rb
198         def exception_page(exception, opts=(no_positional_opts = true; OPTS),
199             assets: opts[:assets],
200             context: opts[:context],
201             css_file: opts[:css_file],
202             js_file: opts[:js_file],
203             json: opts[:json],
204             **kw)
205           RodaPlugins.check_keywords(__callee__, no_positional_opts, opts, kw)
206 
207           message = exception_page_exception_message(exception)
208           if json
209             @_response[RodaResponseHeaders::CONTENT_TYPE] = "application/json"
210             {
211               "exception"=>{
212                 "class"=>exception.class.to_s,
213                 "message"=>message,
214                 "backtrace"=>exception.backtrace.map(&:to_s)
215               }
216             }
217           elsif env['HTTP_ACCEPT'] =~ /text\/html/
218             @_response[RodaResponseHeaders::CONTENT_TYPE] = "text/html"
219 
220             context ||= 7
221 
222             case prefix = assets
223             when false
224               css_file = false if css_file.nil?
225               js_file = false if js_file.nil?
226             when nil
227               # nothing
228             else
229               prefix = '' if prefix == true
230               css_file ||= "#{prefix}/exception_page.css"
231               js_file ||= "#{prefix}/exception_page.js"
232             end
233 
234             css = case css_file
235             when nil
236               "<style type=\"text/css\">#{exception_page_css}</style>"
237             when false
238               # :nothing
239             else
240               "<link rel=\"stylesheet\" href=\"#{h css_file}\" />"
241             end
242 
243             js = case js_file
244             when nil
245               "<script type=\"text/javascript\">\n//<!--\n#{exception_page_js}\n//-->\n</script>"
246             when false
247               # :nothing
248             else
249               "<script type=\"text/javascript\" src=\"#{h js_file}\"></script>"
250             end
251 
252             frames = exception.backtrace.map.with_index do |line, i|
253               frame = {:id=>i}
254               if line =~ /\A(.*?):(\d+)(?::in [`'](.*)')?\Z/
255                 filename = frame[:filename] = $1
256                 lineno = frame[:lineno] = $2.to_i
257                 frame[:function] = $3
258 
259                 begin
260                   lineno -= 1
261                   lines = ::File.readlines(filename)
262                   if line = lines[lineno]
263                     pre_lineno = [lineno-context, 0].max
264                     if (pre_context = lines[pre_lineno...lineno]) && !pre_context.empty?
265                       frame[:pre_context_lineno] = pre_lineno
266                       frame[:pre_context] = pre_context
267                     end
268 
269                     post_lineno = [lineno+context, lines.size].min
270                     if (post_context = lines[lineno+1..post_lineno]) && !post_context.empty?
271                       frame[:post_context_lineno] = post_lineno
272                       frame[:post_context] = post_context
273                     end
274 
275                     frame[:context_line] = line.chomp
276                   end
277                 rescue
278                 end
279 
280                 frame
281               end
282             end.compact
283 
284             r = @_request
285             begin 
286               post_data = r.POST
287               missing_post = "No POST data"
288             rescue
289               missing_post = "Invalid POST data"
290             end
291             info = lambda do |title, id, var, none|
292               <<END
293   <h3 id="#{id}">#{title}</h3>
294   #{(var && !var.empty?) ? (<<END1) : "<p>#{none}</p>"
295     <table class="req">
296       <thead>
297         <tr>
298           <th>Variable</th>
299           <th>Value</th>
300         </tr>
301       </thead>
302       <tbody>
303           #{var.sort_by{|k, _| k.to_s}.map{|key, val| (<<END2)}.join
304           <tr>
305             <td>#{h key}</td>
306             <td class="code"><div>#{h val.inspect}</div></td>
307           </tr>
308 END2
309 }
310       </tbody>
311     </table>
312 END1
313 }
314 END
315             end
316 
317             <<END
318 <!DOCTYPE html>
319 <html lang="en">
320 <head>
321   <meta http-equiv="content-type" content="text/html; charset=utf-8" />
322   <title>#{h exception.class} at #{h r.path}</title>
323   #{css}
324 </head>
325 <body>
326 
327 <div id="summary">
328   <h1>#{h exception.class} at #{h r.path}</h1>
329   <h2>#{h message}</h2>
330   <table><tr>
331     <th>Ruby</th>
332     <td>
333 #{(first = frames.first) ? "<code>#{h first[:filename]}</code>: in <code>#{h first[:function]}</code>, line #{first[:lineno]}" : "unknown location"}
334     </td>
335   </tr><tr>
336     <th>Web</th>
337     <td><code>#{r.request_method} #{h r.host}#{h r.path}</code></td>
338   </tr></table>
339 
340   <h3>Jump to:</h3>
341   <ul id="quicklinks">
342     <li><a href="#get-info">GET</a></li>
343     <li><a href="#post-info">POST</a></li>
344     <li><a href="#cookie-info">Cookies</a></li>
345     <li><a href="#env-info">ENV</a></li>
346   </ul>
347 </div>
348 
349 <div id="traceback">
350   <h2>Traceback <span>(innermost first)</span></h2>
351   <ul class="traceback">
352 #{frames.map{|frame| id = frame[:id]; (<<END1)}.join
353       <li class="frame">
354         <code>#{h frame[:filename]}:#{frame[:lineno]}</code> in <code>#{h frame[:function]}</code>
355 
356           #{frame[:context_line] ? (<<END2) : '</li>'
357           <div class="context" id="c#{id}">
358             #{frame[:pre_context] ? (<<END3) : ''
359             <ol start="#{frame[:pre_context_lineno]+1}" id="bc#{id}">
360               #{frame[:pre_context].map{|line| "<li>#{h line}</li>"}.join}
361             </ol>
362 END3
363 }
364 
365             <ol start="#{frame[:lineno]}" class="context-line">
366               <li>#{h frame[:context_line]}<span>...</span></li>
367             </ol>
368 
369             #{frame[:post_context] ? (<<END4) : ''
370             <ol start='#{frame[:lineno]+1}' id="ac#{id}">
371               #{frame[:post_context].map{|line| "<li>#{h line}</li>"}.join}
372             </ol>
373 END4
374 }
375           </div>
376 END2
377 }
378 END1
379 }
380   </ul>
381 </div>
382 
383 <div id="requestinfo">
384   <h2>Request information</h2>
385 
386   #{info.call('GET', 'get-info', r.GET, 'No GET data')}
387   #{info.call('POST', 'post-info', post_data, missing_post)}
388   #{info.call('Cookies', 'cookie-info', r.cookies, 'No cookie data')}
389   #{info.call('Rack ENV', 'env-info', r.env, 'No Rack env?')}
390 </div>
391 
392 <div id="explanation">
393   <p>
394     You're seeing this error because you use the Roda exception_page plugin.
395   </p>
396 </div>
397 
398 #{js}
399 </body>
400 </html>
401 END
402           else
403             @_response[RodaResponseHeaders::CONTENT_TYPE] = "text/plain"
404             "#{exception.class}: #{message}\n#{exception.backtrace.map{|l| "\t#{l}"}.join("\n")}"
405           end
406         end
exception_page_css()

The CSS to use on the exception page

[show source]
    # File lib/roda/plugins/exception_page.rb
409 def exception_page_css
410   ExceptionPage.css
411 end
exception_page_js()

The JavaScript to use on the exception page

[show source]
    # File lib/roda/plugins/exception_page.rb
414 def exception_page_js
415   ExceptionPage.js
416 end