module Roda::RodaPlugins::ExceptionPage

  1. lib/roda/plugins/exception_page.rb

The exception_page plugin provides an exception_page method that is designed to be called inside the error handler to provide a page to the developer with debugging information. It should only be used in developer environments with trusted clients, as it can leak source code and other information that may be useful for attackers if used in other environments.

Example:

plugin :exception_page
plugin :error_handler do |e|
  next exception_page(e) if ENV['RACK_ENV'] == 'development'
  # ...
end

The exception_page plugin is based on Rack::ShowExceptions, with the following differences:

  • Not a middleware, so it doesn’t handle exceptions itself, and has no effect on the callstack unless the exception_page method is called.

  • Supports external javascript and stylesheets, allowing context toggling to work in applications that use a content security policy to restrict inline javascript and stylesheets (:assets, :css_file, and :js_file options).

  • Has fewer dependencies (does not require ostruct and erb).

  • Sets the Content-Type for the response, and returns the body string, but does not modify other headers or the response status.

  • Supports a configurable amount of context lines in backtraces (:context option).

  • Supports optional JSON formatted output, if used with the json plugin (:json option).

To use the external javascript and stylesheets, you can call r.exception_page_assets in your routing tree:

route do |r|
  # ...

  # serve GET /exception_page.{css,js} requests
  # Use with assets: true +exception_page+ option
  r.exception_page_assets

  r.on "static" do
    # serve GET /static/exception_page.{css,js} requests
    # Use with assets: '/static' +exception_page+ option
    r.exception_page_assets
  end
end

It’s also possible to store the asset information in static files and serve those, you can get the current assets by calling:

Roda::RodaPlugins::ExceptionPage.css
Roda::RodaPlugins::ExceptionPage.js

As the exception_page plugin is based on Rack::ShowExceptions, it is also under rack’s license:

Copyright © 2007-2018 Christian Neukirchen <chneukirchen.org/infopage.html>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The HTML template used in Rack::ShowExceptions was based on Django’s template and is under the following license:

adapted from Django <www.djangoproject.com> Copyright © Django Software Foundation and individual contributors. Used under the modified BSD license: www.xfree86.org/3.3.6/COPYRIGHT2.html#5

Methods

Public Class

  1. css
  2. js
  3. load_dependencies

Public Class methods

css()

Stylesheet used by the HTML exception page

[show source]
    # File lib/roda/plugins/exception_page.rb
 92       def self.css
 93         <<END
 94 html * { padding:0; margin:0; }
 95 body * { padding:10px 20px; }
 96 body * * { padding:0; }
 97 body { font:small sans-serif; }
 98 body>div { border-bottom:1px solid #ddd; }
 99 h1 { font-weight:normal; }
100 h2 { margin-bottom:.8em; }
101 h2 span { font-size:80%; color:#666; font-weight:normal; }
102 h3 { margin:1em 0 .5em 0; }
103 h4 { margin:0 0 .5em 0; font-weight: normal; }
104 table {
105     border:1px solid #ccc; border-collapse: collapse; background:white; }
106 tbody td, tbody th { vertical-align:top; padding:2px 3px; }
107 thead th {
108     padding:1px 6px 1px 3px; background:#fefefe; text-align:left;
109     font-weight:normal; font-size:11px; border:1px solid #ddd; }
110 tbody th { text-align:right; color:#666; padding-right:.5em; }
111 table.vars { margin:5px 0 2px 40px; }
112 table.vars td, table.req td { font-family:monospace; }
113 table td.code { width:100%;}
114 table td.code div { overflow:hidden; }
115 table.source th { color:#666; }
116 table.source td {
117     font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
118 ul.traceback { list-style-type:none; }
119 ul.traceback li.frame { margin-bottom:1em; }
120 div.context { margin: 10px 0; }
121 div.context ol {
122     padding-left:30px; margin:0 10px; list-style-position: inside; }
123 div.context ol li {
124     font-family:monospace; white-space:pre; color:#666; cursor:pointer; }
125 div.context ol.context-line li { color:black; background-color:#ccc; }
126 div.context ol.context-line li span { float: right; }
127 div.commands { margin-left: 40px; }
128 div.commands a { color:black; text-decoration:none; }
129 #summary { background: #ffc; }
130 #summary h2 { font-weight: normal; color: #666; font-family: monospace; white-space: pre-wrap;}
131 #summary ul#quicklinks { list-style-type: none; margin-bottom: 2em; }
132 #summary ul#quicklinks li { float: left; padding: 0 1em; }
133 #summary ul#quicklinks>li+li { border-left: 1px #666 solid; }
134 #explanation { background:#eee; }
135 #traceback { background:#eee; }
136 #requestinfo { background:#f6f6f6; padding-left:120px; }
137 #summary table { border:none; background:transparent; }
138 #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
139 #requestinfo h3 { margin-bottom:-1em; }
140 .error { background: #ffc; }
141 .specific { color:#cc3300; font-weight:bold; }
142 END
143       end
js()

Javascript used by the HTML exception page for context toggling

[show source]
    # File lib/roda/plugins/exception_page.rb
146       def self.js
147         <<END
148 var contexts = document.getElementsByClassName('context');
149 var num_contexts = contexts.length;
150 function toggle() {
151   for (var i = 0; i < arguments.length; i++) {
152     var e = document.getElementById(arguments[i]);
153     if (e) {
154       e.style.display = e.style.display == 'none' ? 'block' : 'none';
155     }
156   }
157   return false;
158 }
159 for (var j = 0; j < num_contexts; j++) {
160   contexts[j].onclick = function(){toggle('b'+this.id, 'a'+this.id);}
161   contexts[j].onclick();
162 }
163 END
164       end
load_dependencies(app)
[show source]
   # File lib/roda/plugins/exception_page.rb
87 def self.load_dependencies(app)
88   app.plugin :h
89 end