【发布时间】:2020-05-12 23:21:26
【问题描述】:
我正在尝试创建一个模块来处理我在include 在我的application_controller.rb 中的 API 错误。这是我模块的self.included 函数。
def self.included(clazz)
clazz.class_eval do
rescue_from ActiveRecord::RecordNotFound do |e|
respond(:record_not_found, 404, e.message)
end
rescue_from ActiveRecord::RecordInvalid do |e|
respond(:record_invalid, 422, e.record.errors.full_messages)
end
rescue_from ActionController::ParameterMissing do |e|
respond(:bad_request, 400, e.message)
end
rescue_from StandardError do |e|
respond(:standard_error, 500, e.message)
end
end
end
我遇到的问题是StandardError 捕获所有错误,包括我在其他rescue_from 块中定义的其他错误。
我想实现这样的目标:
begin
rescue ActiveRecord::RecordNotFound => e
rescue ActiveRecord::RecordInvalid => e
rescue ActionController::ParameterMissing => e
rescue StandardError => e
end
提前感谢您的帮助。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-6