Class: Rack::Rescue::Handler

Inherits:
Object
  • Object
show all
Includes:
Pancake::Mixins::Render
Defined in:
lib/rack/rescue/handler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_formatObject

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_templateObject

Returns the value of attribute default_template.



10
11
12
# File 'lib/rack/rescue/handler.rb', line 10

def default_template
  @default_template
end

#exceptionObject

Returns the value of attribute exception.



10
11
12
# File 'lib/rack/rescue/handler.rb', line 10

def exception
  @exception
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/rack/rescue/handler.rb', line 11

def name
  @name
end

#statusObject

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”

Examples:

# ENV['RACK_ENV'] == "test" && format == :html
my_template.test.html.haml

# The second preference is to use simply the format
my_template.html.haml
# To find a template: my_template.html.erb

See Also:

  • Pancake::Mixins::Render::ClassMethods._template_name_for


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

Examples:

Rack::Rescue::Handler.push_paths(:error_templates, ".", "**/*")
Rack::Rescue::Handler._template_path_name should return :error_templates

See Also:

  • Pancake::Mixins::Render::ClassMethods._template_path_name


54
55
56
# File 'lib/rack/rescue/handler.rb', line 54

def self._template_path_name(opts = {})
  :error_templates
end

.default_formatObject

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

See Also:



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

Examples:

Rack::Rescue::Handler.default_format = :html

See Also:



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

Examples:

Rack::Rescue::Handler.template?(:obscure_error, :format => :html)

Returns:

  • (Boolean)

See Also:

  • Pancake::Mixins::Render::ClassMethods#template


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