Class: Rack::Rescue::Handler
- Inherits:
-
Object
- Object
- Rack::Rescue::Handler
- Includes:
- Pancake::Mixins::Render
- Defined in:
- lib/rack/rescue/handler.rb
Instance Attribute Summary collapse
-
#default_format ⇒ Object
Returns the value of attribute default_format.
-
#default_template ⇒ Object
Returns the value of attribute default_template.
-
#exception ⇒ Object
Returns the value of attribute exception.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
-
._template_name_for(name, opts) ⇒ Object
Provides the name for the template with the relevant options.
-
._template_path_name(opts = {}) ⇒ Object
The defaule path name for the paths_for label.
-
.default_format ⇒ Object
Rack::Rescue::Handler looks for templates in the form <template_name>.<format>.<engine_name>.
-
.default_format=(format) ⇒ Object
Set the default format to format of your chosing.
-
.template?(name, opts = {}) ⇒ Boolean
Looks to se if a template is avaialble.
Instance Method Summary collapse
-
#initialize(exception, opts = {}, &blk) ⇒ Handler
constructor
A new instance of Handler.
-
#render_error(error, opts = {}) ⇒ Object
private
The main workhorse of the handler This should be called with the error you want to render.
Constructor Details
#initialize(exception, opts = {}, &blk) ⇒ Handler
Returns a new instance of Handler.
92 93 94 95 96 97 98 |
# File 'lib/rack/rescue/handler.rb', line 92 def initialize(exception, opts = {}, &blk) @exception = exception @name = Exceptions.exception_name(exception) @status = opts.fetch(:status, 500) @default_template = opts.fetch(:template, 'error') @default_format = opts[:format] if opts[:format] end |
Instance Attribute Details
#default_format ⇒ Object
Returns the value of attribute default_format.
10 11 12 |
# File 'lib/rack/rescue/handler.rb', line 10 def default_format @default_format end |
#default_template ⇒ Object
Returns the value of attribute default_template.
10 11 12 |
# File 'lib/rack/rescue/handler.rb', line 10 def default_template @default_template end |
#exception ⇒ Object
Returns the value of attribute exception.
10 11 12 |
# File 'lib/rack/rescue/handler.rb', line 10 def exception @exception end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/rack/rescue/handler.rb', line 11 def name @name end |
#status ⇒ Object
Returns the value of attribute status.
10 11 12 |
# File 'lib/rack/rescue/handler.rb', line 10 def status @status end |
Class Method Details
._template_name_for(name, opts) ⇒ Object
Provides the name for the template with the relevant options
The template name should be the filename of the template up and until the extension for the template engine.
The template name can be one of two forms. The first preference includes the format, and the rack environment.
Rack::Rescue::Handler._template_name_for(“my_template”, :format => :html) #=> “my_template.html”
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rack/rescue/handler.rb', line 78 def self._template_name_for(name,opts) @template_names ||= {} format = opts.fetch(:format, default_format) env = ENV['RACK_ENV'] key = [name, env, format] @template_names[key] ||= begin names = [] names << "#{name}.#{env}.#{format}" if env && format names << "#{name}.#{format}" if format names << "#{name}" names end end |
._template_path_name(opts = {}) ⇒ Object
The defaule path name for the paths_for label
54 55 56 |
# File 'lib/rack/rescue/handler.rb', line 54 def self._template_path_name(opts = {}) :error_templates end |
.default_format ⇒ Object
Rack::Rescue::Handler looks for templates in the form
<template_name>.<format>.<engine_name>
By default, the format is :text, but you can configure this to be any format you like
19 20 21 |
# File 'lib/rack/rescue/handler.rb', line 19 def self.default_format @default_format ||= :text end |
.default_format=(format) ⇒ Object
Set the default format to format of your chosing
30 31 32 |
# File 'lib/rack/rescue/handler.rb', line 30 def self.default_format=(format) @default_format = format end |
.template?(name, opts = {}) ⇒ Boolean
Looks to se if a template is avaialble
40 41 42 43 44 |
# File 'lib/rack/rescue/handler.rb', line 40 def self.template?(name, opts = {}) !!template(name, opts) rescue Pancake::Mixins::Render::TemplateNotFound false end |
Instance Method Details
#render_error(error, opts = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The main workhorse of the handler This should be called with the error you want to render. The error will be provided to the template in the local “error” variable
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rack/rescue/handler.rb', line 104 def render_error(error, opts = {}) opts = opts.dup template_name = opts.fetch(:template_name, default_template) opts[:format] ||= default_format || self.class.default_format opts[:error] ||= error opts[:status] = self.status if self.class.template?(template_name, opts) tn = template_name else tn = 'error' unless self.class.template?(tn, opts) opts[:format] = :text end end render(tn, opts) end |