The error_handler plugin adds an error handler to the routing tree, so that if routing the request raises an error, a nice error message page (or API response) can be returned to the user.
You should provide the error handler as a block to the plugin:
plugin :error_handler do |e| "Oh No!" end
If an error occurs, the exception instance is passed into the block, and the block can return the request body via a string (or another configured block result).
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 argument to the plugin:
plugin :error_handler, classes: [StandardError, ScriptError, NoMemoryError]
Classes and Modules
Constants
| DEFAULT_ERROR_HANDLER_CLASSES | = | [StandardError, ScriptError].freeze |
Public Class methods
If a block is given, use it to setup the error handler.
# File lib/roda/plugins/error_handler.rb 43 def self.configure(app, opts=(no_positional_opts = true; OPTS), classes: opts[:classes], **kw, &block) 44 RodaPlugins.check_keywords("the error_handler plugin", no_positional_opts, opts, kw) 45 app.opts[:error_handler_classes] = (classes || app.error_handler_classes || DEFAULT_ERROR_HANDLER_CLASSES).dup.freeze 46 47 if block 48 app.class_exec do 49 define_method(:handle_error, &block) 50 alias_method(:handle_error, :handle_error) 51 private :handle_error 52 end 53 end 54 end