module Grape::DSL::RequestResponse::ClassMethods

def rescue_from(*args, &block)

Parameters:
  • handler (Proc) -- Execution proc to handle the given exception as an
  • options (Hash) -- Options for the rescue usage.
  • block (Block) -- Execution block to handle the given exception.
  • exception_classes (Array) -- A list of classes that you want to rescue, or

Options Hash: (**options)
  • :rescue_subclasses (Boolean) -- Also rescue subclasses of exception classes
  • :backtrace (Boolean) -- Include a backtrace in the rescue response.

Overloads:
  • rescue_from(*exception_classes, **options)

Other tags:
    Example: Rescue from custom exceptions -
def rescue_from(*args, &block)
  if args.last.is_a?(Proc)
    handler = args.pop
  elsif block
    handler = block
  end
  options = args.extract_options!
  raise ArgumentError, 'both :with option and block cannot be passed' if block && options.key?(:with)
  handler ||= extract_with(options)
  if args.include?(:all)
    namespace_inheritable(:rescue_all, true)
    namespace_inheritable(:all_rescue_handler, handler)
  elsif args.include?(:grape_exceptions)
    namespace_inheritable(:rescue_all, true)
    namespace_inheritable(:rescue_grape_exceptions, true)
    namespace_inheritable(:grape_exceptions_rescue_handler, handler)
  else
    handler_type =
      case options[:rescue_subclasses]
      when nil, true
        :rescue_handlers
      else
        :base_only_rescue_handlers
      end
    namespace_reverse_stackable(handler_type, args.to_h { |arg| [arg, handler] })
  end
  namespace_stackable(:rescue_options, options)
end