Class: Rack::Rescue::Exceptions

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/rescue/exceptions.rb,
lib/rack/rescue/default_exceptions.rb

Constant Summary collapse

DEFAULT_HANDLERS =
[
 ["RuntimeError",                            {:status => 500}],
 ["DataMapper::ObjectNotFoundError",         {:status => 404}],
 ["ActiveRecord::RecordNotFound",            {:status => 404}],
 ["Pancake::Errors::NotFound",               {:status => 404, :template => :not_found}],
 ["Pancake::Errors::UnknownRouter",          {:status => 500}],
 ["Pancake::Errors::UnknownConfiguration",   {:status => 500}],
 ["Pancake::Errors::Unauthorized",           {:status => 401}],
 ["Pancake::Errors::Forbidden",              {:status => 403}],
 ["Pancake::Errors::Server",                 {:status => 500}],
 ["Pancake::Errors::NotAcceptable",          {:status => 406}]
].map{|(name, opts)| Rack::Rescue::Handler.new(name, opts)}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(load_defaults = true) ⇒ Exceptions

Returns a new instance of Exceptions.

Parameters:

  • load_defaults (defaults to: true)

    Load the default list of exceptions into this instance



45
46
47
48
# File 'lib/rack/rescue/exceptions.rb', line 45

def initialize(load_defaults = true)
  @exception_handlers = {}
  load_defaults! if load_defaults
end

Class Method Details

.add_defaults(*exceptions) ⇒ Object

Add default exceptions to handle.

Examples:

Rack::Rescue::Exceptions.add_defaults("MyException", "AnotherException", :status => 404)

See Also:



13
14
15
16
17
18
# File 'lib/rack/rescue/exceptions.rb', line 13

def self.add_defaults(*exceptions)
  opts = Hash === exceptions.last ? exceptions.pop : {}
  exceptions.each do |e|
    DEFAULT_HANDLERS << Handler.new(e, opts)
  end
end

.exception_name(e) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/rack/rescue/exceptions.rb', line 91

def self.exception_name(e)
  case e
  when String
    e
  when Class
    e.name
  else
    e.class.name
  end
end

.remove_defaults(*exceptions) ⇒ Object

Remove a deafult exception from the list

Examples:

Rack::Rescue::Exceptions.remove_defaults("MyException", "AnotherException")
Rack::Rescue::Exceptions.remove_defaults(/MyKindOfException/)

See Also:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/rescue/exceptions.rb', line 28

def self.remove_defaults(*exceptions)
  removed = []
  exceptions.each do |e|
    DEFAULT_HANDLERS.each do |h|
      match = case  e
      when String
        h.name == e
      when Regexp
        h.name =~ e
      end
      removed << h if match
    end
  end
  removed.each{|r| DEFAULT_HANDLERS.delete(r) }
end

Instance Method Details

#[](exception) ⇒ Object



87
88
89
# File 'lib/rack/rescue/exceptions.rb', line 87

def [](exception)
  exception_handlers[exception_name(exception)]
end

#add(*exceptions) ⇒ Object

Add an exception handler to Rack::Rescue. Whenever Rack::Rescue rescues an exception, it will check it’s list to see what to do with it.

Parameters:

  • exceptions

    a list of exceptions with an optional options hash on the end

  • :status (Hash)

    a customizable set of options

See Also:



69
70
71
72
73
74
75
# File 'lib/rack/rescue/exceptions.rb', line 69

def add(*exceptions)
  opts = Hash === exceptions.last ? exceptions.pop : {}
  exceptions.each do |e|
    name = exception_name(e)
    exception_handlers[name] = Handler.new(e, opts)
  end
end

#delete(e) ⇒ Object

Remove an exception handler from this instance of Rack::Rescue::Exceptions This will not remove a default handler



79
80
81
# File 'lib/rack/rescue/exceptions.rb', line 79

def delete(e)
  exception_handlers.delete(exception_name(e))
end

#exception_handlersObject



83
84
85
# File 'lib/rack/rescue/exceptions.rb', line 83

def exception_handlers
  @exception_handlers
end

#exception_name(e) ⇒ Object



102
103
104
# File 'lib/rack/rescue/exceptions.rb', line 102

def exception_name(e)
  self.class.exception_name(e)
end

#load_defaults!Object

Loads the default list of exceptions defined in Rack::Rescue::Exceptions::DEFAULT_HANDLERS



53
54
55
56
57
# File 'lib/rack/rescue/exceptions.rb', line 53

def load_defaults!
  DEFAULT_HANDLERS.each do |handler|
    exception_handlers[handler.name] = handler
  end
end