module Roda::RodaPlugins::ErrorHandler

  1. lib/roda/plugins/error_handler.rb

The error_handler plugin adds an error handler to the routing, so that if routing the request raises an error, a nice error message page can be returned to the user.

You can provide the error handler as a block to the plugin:

plugin :error_handler do |e|
  "Oh No!"
end

Or later via the error class method:

plugin :error_handler

error do |e|
  "Oh No!"
end

In both cases, the exception instance is passed into the block, and the block can return the request body via a string.

If an exception is raised, a new response will be used, with the default status set to 500, before executing the error handler. The error handler can change the response status if necessary, as well set headers and/or write to the body, just like a regular request. After the error handler returns a response, normal after processing of that response occurs, except that an exception during after processing is logged to env['rack.errors'] but otherwise ignored. This avoids recursive calls into the error_handler. Note that if the error_handler itself raises an exception, the exception will be raised without normal after processing. This can cause some after processing to run twice (once before the error_handler is called and once after) if later after processing raises an exception.

By default, this plugin handles StandardError and ScriptError. To override the exception classes it will handle, pass a :classes option to the plugin:

plugin :error_handler, classes: [StandardError, ScriptError, NoMemoryError]

Methods

Public Class

  1. configure

Constants

DEFAULT_ERROR_HANDLER_CLASSES = [StandardError, ScriptError].freeze  

Public Class methods

configure(app, opts={}, &block)

If a block is given, automatically call the error method on the Roda class with it.

[show source]
   # File lib/roda/plugins/error_handler.rb
51 def self.configure(app, opts={}, &block)
52   app.opts[:error_handler_classes] = (opts[:classes] || app.opts[:error_handler_classes] || DEFAULT_ERROR_HANDLER_CLASSES).dup.freeze
53 
54   if block
55     app.error(&block)
56   end
57 end