ruby-grape / grape

An opinionated framework for creating REST-like APIs in Ruby.
http://www.ruby-grape.org
MIT License
9.88k stars 1.22k forks source link

Grape + Cucumber | number of arguments #2335

Open rrmartins opened 1 year ago

rrmartins commented 1 year ago

hello, in my tests with cucumber I have a problem.

I'm getting a wrong number of arguments (given 1, expected 0) (ArgumentError) error when I try to make a request.

I make a request that is missing parameters, I should get a 400 error with the missing parameter message.

    params do
      requires :field_a, type: Integer
      requires :field_b, type: Integer
      requires :field_c, type: Float
      requires :field_d, type: Float
      optional :active, type: Boolean
    end
    post do
      result = WorkitProductCategory.create_or_update(params)
      present result, with: Api::V1::Entities::Pmp::WorkitProductCategoryObject
    end

so is my rescue_from:

  rescue_from Grape::Exceptions::ValidationErrors do |e|
      Rails.logger.debug(e)
      error!({ error: e.message, class: e.class.name, errors: e.errors }, e.status)
    end
dblock commented 1 year ago

What's the complete call stack?

rrmartins commented 1 year ago

I found the problem. inside the Grape::Exceptions::ValidationErrors return block, I had a Logger.debug. When doing an inspection of the running code, I noticed that there is something strange with the Rails.Logger.debug when the ValidationErrors object was sent. Then I realized that it was necessary to pass a "string" and not an object to be rendered.

# Before:
rescue_from Grape::Exceptions::ValidationErrors do |e|
      error = { error: e.message, class: e.class.name, errors: e.errors }

      Rails.logger.debug(e)

      Rack::Response.new(error.to_json, e.status)
end
# After:
rescue_from Grape::Exceptions::ValidationErrors do |e|
      error = { error: e.message, class: e.class.name, errors: e.errors }

      Rails.logger.debug(error.to_json) <----- HERE

      Rack::Response.new(error.to_json, e.status)
end
dblock commented 1 year ago

This could still be a bug. I expect Logger.debug(e) to work for Grape::Exceptions::ValidationErrors. Any interest in writing a spec for it and seeing if this is the case for all errors or just some errors?