葡萄错误处理策略? [英] Grape error handling strategy?

查看:41
本文介绍了葡萄错误处理策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Grape 和 Rails 创建 REST API.我已经有了基本的架构,我正在寻找可以清理"事物的地方.其中之一是错误处理/处理.

I am using Grape and Rails to create a REST API. I have the basic architecture in place and I am looking for places to 'clean' things up. One of those places is the error handling/processing.

我目前正在修复整个 API 的 root.rb(GRAPE::API 基类)文件中的错误.我格式化它们,然后通过rack_response 将错误发回.一切正常,但 root.rb 文件变得有点臃肿,所有错误都被挽救了,其中一些需要进行特殊解析.我想知道是否有人开发了一个好的错误处理策略,以便可以将它移到它自己的模块中,并使 root.rb(GRAPE::API 基类)相当精简.

I am currently rescuing errors in the root.rb (GRAPE::API base class) file for the whole API. I format them and then send the error back via rack_response. Everything works find but the root.rb file is getting a bit bloated with all the errors being rescued and some of them have special parsing that needs to be done. I was wondering if anyone has developed a good strategy for error handling so that it can be moved out into it's own module and leave the root.rb (GRAPE::API base class) fairly lean.

我真的很想创建一个错误处理模块并为每种类型的错误定义方法,例如...

I would really like to create a error processing module and define methods for each type of error, for example...

module API
 module ErrorHandler
   def record_not_found
     rack_response API::Utils::ApiErrors.new({type: e.class.name, message: 'Record not found'}).to_json, 404
   end
 end
end

然后在 root.rb 文件中做这样的事情

Then in the root.rb file do something like this

module API
  class Root < Grape::API
    prefix 'api'
    format :json

    helpers API::ErrorHandler

    rescue_from ActiveRecord::RecordNotFound, with: :record_not_found # Use the helper method as the handler for this error
  end
end

有人做过这样的事情吗?我一直在尝试上述策略的各种风格,但我似乎无法得到任何工作.

Has anyone done something like this? I have been trying various flavors of the above strategy but I can't seem to get anything work.

推荐答案

我得出以下解决方案/策略...

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

然后我只是在我的基本 GRAPE::API 类中包含错误

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

经过大量实验和许多其他尝试都不起作用,我认为这是一个很好的解决方案,我的基础 GRAPE::API 类仍然非常精简.我仍然对人们可能采用的任何其他方法持开放态度.

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.

这篇关于葡萄错误处理策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆