Grape error handling strategy?

让人想犯罪 __ 提交于 2019-12-05 04:06:32
mfunaro

I've come to the following solution/strategy...

I moved all error rescuing to its own module like the following

module API
  module Errors
    extend ActiveSupport::Concern

    included do
      rescue_from :all do |e|
        rack_response API::Utils::ApiErrors.new({type: e.class.name, message: e.message}).to_json, 500
      end
      .
      .
      .
  end
end

Then I simply include the errors in my base GRAPE::API class

module API
  class Root < Grape::API
    include API::Errors

    prefix 'api'
    format :json

    helpers API::Utils::Helpers::IndexHelpers
    helpers API::Utils::Helpers::WardenHelpers
    helpers API::Utils::Helpers::RecordHelpers
    .
    .
    .
  end
end

After a lot of experimentation and a lot of other attempts not working, I think this is a fine solution and my base GRAPE::API class remains pretty lean. I am still very open to any other approaches people might have.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!