我试图在我的rails应用程序中通知来自Grape的异常,但它不起作用。
# api_error_handler.rb
module API
module V1
module Config
class ApiErrorHandler < Grape::Middleware::Base
def call!(env)
@env = env
begin
@app.call(@env)
rescue StandardError => e
Honeybadger.notify(e)
end
end
end
end
end
end
# In defaults.rb
module API
module V1
module Defaults
extend ActiveSupport::Concern
included do
error_formatter :json, API::V1::Config::ErrorFormatter
rescue_from :all, backtrace: true
use API::V1::Config::ApiErrorHandler
helpers do
def authenticate_user!
...
end
end
end
.....
end
end
end我在每个特定的api中都包含了defaults.rb。
谢谢你的帮助!
发布于 2016-12-19 20:18:54
异常永远不会传播到API::V1::Config::ApiErrorHandler,因为它们会被rescue_from :all, backtrace: true吞并。
您可以在rescue_from块中进行自己的异常处理。
rescue_from :all do |e|
Honeybadger.notify(e)
# Error still formatted with error_formatter
# message, status, headers, backtrace
error! e.message, 500, {}, e.backtrace
end资料来源:
rescue_from :all:Grape documentationerror!接口:Grape sourcehttps://stackoverflow.com/questions/40398325
复制相似问题