ruby-grape/grape · error · ArgumentError
with: #{with.class}, expected Symbol, String or Proc
Error message
with: #{with.class}, expected Symbol, String or Proc What it means
The `with:` option of `rescue_from` must be a handler reference Grape can invoke: a Proc, a Symbol naming an endpoint method, or a String that is converted with to_sym. Any other class (Integer, Hash, Class, nil-replacement objects) raises ArgumentError with the offending class name, since Grape cannot dispatch it when an exception is raised at runtime.
Source
Thrown at lib/grape/dsl/request_response.rb:154
def represent(model_class, with:)
raise Grape::Exceptions::InvalidWithOptionForRepresent.new unless with.is_a?(Class)
inheritable_setting.add_representation(model_class, with)
end
private
def extract_handler(args, with:, block:)
raise ArgumentError, 'both :with option and block cannot be passed' if block && with
return args.pop if args.last.is_a?(Proc)
return block if block
return unless with
case with
when Proc, Symbol then with
when String then with.to_sym
else raise ArgumentError, "with: #{with.class}, expected Symbol, String or Proc"
end
end
end
end
end
View on GitHub (pinned to 22d7975629)
Solutions
- Pass a Symbol naming a defined helper/instance method: `with: :render_error`.
- Pass a Proc/lambda: `with: ->(e) { error!(e.message, 500) }` (String method names are also accepted and to_sym-ed).
- If you need extra data, close over it in the Proc or define a dedicated method instead of passing a Hash.
Example fix
# before
rescue_from ArgumentError, with: { code: 400, msg: 'bad' } # raises with: Hash
# after
rescue_from ArgumentError, with: ->(e) { error!(e.message, 400) }
# or
rescue_from ArgumentError, with: :render_error
def render_error(e); error!({ code: 400, msg: e.message }, 400); end Defensive patterns
Strategy: type-guard
Validate before calling
def valid_with_option?(with) = with.is_a?(Proc) || with.is_a?(Symbol) || with.is_a?(String) raise ArgumentError, 'with: must be Symbol, String, or Proc' unless valid_with_option?(my_with)
Type guard
def handler_ref?(value) = value.is_a?(Proc) || value.is_a?(Symbol) || value.is_a?(String)
Prevention
- Pass `with: :method_name` or `with: ->(e) {}` only - never data structures.
- Freeze a set of known handler names and check dynamic values against it.
- Boot specs catch bad `with:` values at definition time.
When it happens
Trigger: `rescue_from ArgumentError, with: 42` or `with: { message: 'err' }`. `with: SomeModule` (passing a class instead of a Proc). `with: 'render_error '`-style values are fine as Strings, but a frozen numeric/boolean option raises.
Common situations: Passing configuration hashes or objects intended for the handler body instead of a handler reference. Typos like `with: :render_errorrs`. Dynamically computing the handler and accidentally passing nil-replacement or non-callable values.
Related errors
- both :with option and block cannot be passed
- Status code must be Integer or Symbol.
- Argument must be a file path
- rescue_from #{meta_selector.inspect} does not accept additio
- Returning or throwing a Hash from a rescue handler is deprec
AI-assisted analysis of ruby-grape/grape@22d7975629 (2026-08-21).
Data as JSON: /api/errors/f2eb58470d33f673.
Report an issue: GitHub.