@@ -105,9 +105,15 @@ def default_error_status(new_status = nil)
105105 # @overload rescue_from(*exception_classes, options = {})
106106 # @param [Array] exception_classes A list of classes that you want to rescue, or
107107 # the symbol :all to rescue from all exceptions.
108+ # @param [Block] block Execution block to handle the given exception.
108109 # @param [Hash] options Options for the rescue usage.
109110 # @option options [Boolean] :backtrace Include a backtrace in the rescue response.
110- def rescue_from ( *args )
111+ def rescue_from ( *args , &block )
112+ if block_given?
113+ args . each do |arg |
114+ set ( :rescue_handlers , { arg => block } )
115+ end
116+ end
111117 set ( :rescue_options , args . pop ) if args . last . is_a? ( Hash )
112118 set ( :rescue_all , true ) and return if args . include? ( :all )
113119 set ( :rescued_errors , args )
@@ -159,7 +165,7 @@ def http_basic(options = {}, &block)
159165
160166 def http_digest ( options = { } , &block )
161167 options [ :realm ] ||= "API Authorization"
162- options [ :opaque ] ||= "secret"
168+ options [ :opaque ] ||= "secret"
163169 auth :http_digest , options , &block
164170 end
165171
@@ -262,9 +268,10 @@ def build_endpoint(&block)
262268 :rescue_all => settings [ :rescue_all ] ,
263269 :rescued_errors => settings [ :rescued_errors ] ,
264270 :format => settings [ :error_format ] || :txt ,
265- :rescue_options => settings [ :rescue_options ]
266- b . use Rack ::Auth ::Basic , settings [ :auth ] [ :realm ] , &settings [ :auth ] [ :proc ] if settings [ :auth ] && settings [ :auth ] [ :type ] == :http_basic
267- b . use Rack ::Auth ::Digest ::MD5 , settings [ :auth ] [ :realm ] , settings [ :auth ] [ :opaque ] , &settings [ :auth ] [ :proc ] if settings [ :auth ] && settings [ :auth ] [ :type ] == :http_digest
271+ :rescue_options => settings [ :rescue_options ] ,
272+ :rescue_handlers => settings [ :rescue_handlers ] || { }
273+ b . use Rack ::Auth ::Basic , settings [ :auth ] [ :realm ] , &settings [ :auth ] [ :proc ] if settings [ :auth ] && settings [ :auth ] [ :type ] == :http_basic
274+ b . use Rack ::Auth ::Digest ::MD5 , settings [ :auth ] [ :realm ] , settings [ :auth ] [ :opaque ] , &settings [ :auth ] [ :proc ] if settings [ :auth ] && settings [ :auth ] [ :type ] == :http_digest
268275 b . use Grape ::Middleware ::Prefixer , :prefix => prefix if prefix
269276 b . use Grape ::Middleware ::Versioner , :versions => ( version if version . is_a? ( Array ) ) if version
270277 b . use Grape ::Middleware ::Formatter , :default_format => default_format || :json
0 commit comments