1

I write requires,like this

params do
    requires :user_id, :type => Integer
    optional :page, :type => Integer, :default => 1
    optional :per_page, :type => Integer, :default => 20
end

and I have wrote rescue_from

rescue_from :all do |e|
  case e
  when Grape::Exceptions::ValidationErrors
    error!({ messages: e.full_messages }, 400)
  else
    Rails.logger.error "Api Error: #{e}"
    Rails.logger.error "#{e.backtrace.join("\n")}"
    error!({ messages: "errors" }, 500)
  end
end

but I cannot catch Grape::Exceptions::ValidationErrors without params[:user_id]

fcce
  • 1,034
  • 1
  • 12
  • 24
  • What do you mean when you say `but it doesn't works for Grape::Exceptions::ValidationErrors`? What do you expect ? – chumakoff Aug 18 '15 at 15:44
  • I cannot catch Grape::Exceptions::ValidationErrors without params[:user_id], the Validation doesn't work – fcce Aug 18 '15 at 15:58

1 Answers1

0

I don't know why your code doesn't work, but I can offer another working solution: rescue validation errors explicitly (above the rescue_from :all).

rescue_from Grape::Exceptions::ValidationErrors do |e|
  error!({ messages: e.full_messages }, 400)
end

rescue_from :all do |e|
  Rails.logger.error "Api Error: #{e}"
  Rails.logger.error "#{e.backtrace.join("\n")}"
  error!({ messages: "errors" }, 500)
end
chumakoff
  • 6,807
  • 2
  • 23
  • 45
  • `Started GET "/client/v4/addresses?user_id=-199999" for 127.0.0.1 at 2015-08-19 00:28:59 +0800` `Started GET "/client/v4/addresses" for 127.0.0.1 at 2015-08-19 09:49:15 +0800` ,Nothing in log – fcce Aug 19 '15 at 01:50
  • Nothing to say. I will try to help you if you public and give me the link to the Grape source file that consists this code. – chumakoff Aug 19 '15 at 07:31
  • It‘s a mistake by myself, I write a method named 'fail' – fcce Sep 17 '15 at 13:23