Search code examples
ruby-on-railsrubyruby-on-rails-4grape-api

Rails Web Service with Grape, returning vague error


I am trying to create an API web service in Rails using Grape. I followed this tutorial.

I managed to get everything up and running and expected it to work perfectly. When I try to test my API endpoint however, I'm presented with a vague error. I am not sure where to start, as I don't know how to interpret the error returned. Here it is:

compared with non class/module

def rescuable?(klass)
  options[:rescue_all] || (options[:rescue_handlers] || []).any? { |error, _handler| klass <= error } || (options[:base_only_rescue_handlers] || []).include?(klass)
end

Here are the 20 last entries in my trace:

grape (0.13.0) lib/grape/middleware/error.rb:50:in `<='
grape (0.13.0) lib/grape/middleware/error.rb:50:in `block in rescuable?'
grape (0.13.0) lib/grape/middleware/error.rb:50:in `any?'
grape (0.13.0) lib/grape/middleware/error.rb:50:in `rescuable?'
grape (0.13.0) lib/grape/middleware/error.rb:30:in `rescue in call!'
grape (0.13.0) lib/grape/middleware/error.rb:25:in `call!'
grape (0.13.0) lib/grape/middleware/base.rb:18:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
rack (1.6.4) lib/rack/builder.rb:153:in `call'
grape (0.13.0) lib/grape/endpoint.rb:202:in `call!'
grape (0.13.0) lib/grape/endpoint.rb:190:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:68:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
grape (0.13.0) lib/grape/api.rb:114:in `call'
grape (0.13.0) lib/grape/api.rb:44:in `call!'
grape (0.13.0) lib/grape/api.rb:39:in `call'
actionpack (4.2.2) lib/action_dispatch/routing/mapper.rb:51:in `serve'

Can anybody please help me understand what's going on here?


Solution

  • In the expression klass <= error, error is not a class or module, so that comparison is failing. Instead, you want to check if error.kind_of? klass (or perhaps error.instance_of? klass if you want to ensure that error is an instance of that exact class. See https://stackoverflow.com/a/3893305/1106267 for more on that distinction.

    EDIT:

    If you followed the linked tutorial verbatim, it's possible you're using deprecated methods in grape. According to the upgrade docs, error_response (used in the tutorial to generate a custom response for ActiveRecord::RecordNotFound and ActiveRecord::RecordInvalid errors) should be replaced with error! if you're using grape >= 0.12.