2

我的 Grape 应用程序有几个错误处理程序,最后包括:

rescue_from :all, backtrace: true do |e|
  message = { errors: { all: e.message } }
  rack_response(format_message(message, e.backtrace), 500 )
end

但这至少不能挽救 Grape 处理的错误

throw :error

内部。我该如何挽救这些错误?指出的特定错误是“不支持请求的格式'txt'”和“未找到:some_path”。当格式扩展名丢失或只有“。”时,会发生这些错误。分别提供。

4

1 回答 1

1

你不拯救抛出的条件。他们将直接进入错误处理程序,因为rescue是针对raised 错误,而不是抛出条件。throw不会创建与 完全相同的对象raise,并且不能以相同的方式处理。

但是,您可以使用以下格式格式化错误消息error_formatter

module CustomErrorFormatter
  def self.call message, backtrace, options, env
     { errors: { all: message.to_s } }.to_json
  end
end

在主应用程序中:

error_formatter :json, CustomErrorFormatter
于 2014-02-19T08:09:43.670 回答