return Grape rescue_from :all 应该挡住什么?

What should return the Grape rescue_from :all block?

Grape 文档说:
The rescue_from block must return a Rack::Response object, call error! or re-raise an exception.

但是如果我们只使用 rescue_from 方法来记录事情并且我们想保留原始的 HTTP 响应我们应该 return 什么?

如果我重新抛出捕获的异常,它不会保持原来的行为。 例如,我的一些测试会生成 400 响应,但具有:

rescue_from :all do |e|
  Rails.logger.error(e)
  raise e
end

他们收到 500 回复。

我发现重现原始行为的更好解决方案是这段代码:

rescue_from :all do |e|
  Rails.logger.error(e)
  if e.respond_to?(:status)
    error!(e.to_json, e.status, e.headers, e.backtrace)
  else
    error! "Internal Server Error"
  end
end

我应该检查捕获的异常是否响应 status,因为它可能是一个 RuntimeError,而 RuntimeError 不响应 status

我这样做对吗?或者有更好的解决方案吗?

这在 https://github.com/ruby-grape/grape/pull/939, pointed from https://github.com/ruby-grape/grape/issues/1300 中得到了回答。