ruby-grape/grape · error · ArgumentError

rescue_from #{meta_selector.inspect} does not accept additio

Error message

rescue_from #{meta_selector.inspect} does not accept additional arguments

What it means

`rescue_from` accepts special meta-selectors (:all, :grape_exceptions, :internal_grape_exceptions) that configure a whole class of handlers, plus regular exception classes. A meta-selector configures a distinct handler set, so mixing it with exception classes in the same call (`rescue_from :all, CustomError`) is ambiguous and raises ArgumentError at boot.

Source

Thrown at lib/grape/dsl/request_response.rb:100

      # @overload rescue_from(*exception_classes, **options)
      #   @param [Array] exception_classes A list of classes that you want to rescue, or
      #     one of the meta selectors +:all+, +:grape_exceptions+,
      #     +:internal_grape_exceptions+. Meta selectors must be used alone;
      #     mixing with exception classes raises +ArgumentError+.
      #   @param [Block] block Execution block to handle the given exception.
      #   @param [Proc] with Execution proc to handle the given exception as an alternative
      #     to passing a block.
      #   @param [Boolean] rescue_subclasses Also rescue subclasses of exception classes;
      #     defaults to +true+.
      #   @param [Boolean] backtrace Include the rescued exception's backtrace in the
      #     rescue response body.
      #   @param [Boolean] original_exception Include +inspect+ of the rescued exception
      #     in the rescue response body.
      def rescue_from(*args, with: nil, rescue_subclasses: true, backtrace: false, original_exception: false, &block)
        handler = extract_handler(args, with:, block:)
        meta_selector = (args & META_RESCUE_SELECTORS).first
        raise ArgumentError, "rescue_from #{meta_selector.inspect} does not accept additional arguments" if meta_selector && args.size > 1

        case meta_selector
        when :all
          inheritable_setting.add_all_rescue_handler(handler)
        when :grape_exceptions
          inheritable_setting.add_grape_exceptions_rescue_handler(handler)
        when :internal_grape_exceptions
          inheritable_setting.add_internal_grape_exceptions_rescue_handler(handler)
        else
          inheritable_setting.add_rescue_handlers(args.to_h { |klass| [klass, handler] }, subclasses: rescue_subclasses)
        end

        inheritable_setting.add_rescue_options(RescueOptions.new(backtrace:, original_exception:))
      end

      # Allows you to specify a default representation entity for a
      # class. This allows you to map your models to their respective
      # entities once and then simply call `present` with the model.

View on GitHub (pinned to 22d7975629)

Solutions

  1. Split into separate calls: one for the meta-selector, one for the exception classes.
  2. Order them deliberately - specific `rescue_from SomeError` handlers and `rescue_from :all` can coexist as distinct registrations.

Example fix

# before
rescue_from :all, ArgumentError do |e| ... end # raises

# after
rescue_from ArgumentError do |e| ... end
rescue_from :all do |e| ... end
Defensive patterns

Strategy: validation

Validate before calling

META_SELECTORS = %i[all grape_exceptions internal_grape_exceptions].freeze

def assert_single_rescue_from_arg(*args)
  meta = args & META_SELECTORS
  raise ArgumentError, "#{meta.first.inspect} must be alone" if meta.any? && args.size > 1
end

Prevention

When it happens

Trigger: `rescue_from :all, ArgumentError do |e| ... end`. `rescue_from :grape_exceptions, NotFoundError, with: :render`. Any call where one positional arg is a meta-selector symbol and args.size > 1.

Common situations: Gradually tightening error handling by adding specific classes next to an existing `rescue_from :all`. Copy-pasting a rescue_from line and appending another class. Using :grape_exceptions for the structured error format while also wanting domain errors in one call.

Related errors


AI-assisted analysis of ruby-grape/grape@22d7975629 (2026-08-21). Data as JSON: /api/errors/0692081d337126e2. Report an issue: GitHub.