Ruby Проверка винограда не работает

Я пишу requires вот так

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

и я написал 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

но я не могу поймать Grape::Exceptions::ValidationErrors без params[:user_id]


person feng ce    schedule 18.08.2015    source источник
comment
Что вы имеете в виду, когда говорите but it doesn't works for Grape::Exceptions::ValidationErrors? Что вы ожидаете ?   -  person chumakoff    schedule 18.08.2015
comment
Я не могу поймать Grape::Exceptions::ValidationErrors без params[:user_id], проверка не работает   -  person feng ce    schedule 18.08.2015


Ответы (1)


Я не знаю, почему ваш код не работает, но я могу предложить другое рабочее решение: явным образом спасать ошибки проверки (над спасением_из :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
person chumakoff    schedule 18.08.2015
comment
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 ,В логе ничего - person feng ce; 19.08.2015
comment
Нечего сказать. Я постараюсь помочь вам, если вы опубликуете и дайте мне ссылку на исходный файл Grape, который состоит из этого кода. - person chumakoff; 19.08.2015
comment
Это моя ошибка, я пишу метод с именем «сбой» - person feng ce; 17.09.2015