lib/rack/api/runner.rb in rack-api-0.2.2 vs lib/rack/api/runner.rb in rack-api-0.3.0
- old
+ new
@@ -2,25 +2,27 @@
class API
class Runner
HTTP_METHODS = %w[get post put delete head]
DELEGATE_METHODS = %w[
- version use prefix basic_auth
+ version use prefix basic_auth rescue_from
helper respond_to default_url_options
]
attr_accessor :settings
def initialize
@settings = {
:middlewares => [],
:helpers => [],
+ :rescuers => [],
:global => {
- :prefix => "/",
- :formats => %w[json jsonp],
+ :prefix => "/",
+ :formats => %w[json jsonp],
:middlewares => [],
- :helpers => []
+ :helpers => [],
+ :rescuers => []
}
}
end
# Set configuration based on scope. When defining values outside version block,
@@ -232,10 +234,23 @@
route("#{method.upcase}", *args, &block) # route("GET", *args, &block)
end # end
RUBY
end
+ # Rescue from the specified exception.
+ #
+ # rescue_from ActiveRecord::RecordNotFound, :status => 404
+ # rescue_from Exception, :status => 500
+ # rescue_from Exception do
+ # $logger.error error.inspect
+ # [500, {"Content-Type" => "text/plain"}, []]
+ # end
+ #
+ def rescue_from(exception, options = {}, &block)
+ set :rescuers, {:class_name => exception.name, :options => options, :block => block}, :append
+ end
+
private
def mount_path(path) # :nodoc:
Rack::Mount::Utils.normalize_path([option(:prefix), settings[:version], path].join("/"))
end
@@ -247,10 +262,11 @@
app = App.new({
:handler => handler,
:default_format => default_format,
:version => option(:version),
:prefix => option(:prefix),
- :url_options => option(:url_options)
+ :url_options => option(:url_options),
+ :rescuers => option(:rescuers, :merge).reverse
})
builder = Rack::Builder.new
# Add middleware for basic authentication.