【问题标题】:How would one make multiple exception classes inherit from one parent class?如何让多个异常类从一个父类继承?
【发布时间】:2017-08-06 01:26:15
【问题描述】:

到目前为止,我有这个......

class MembersController < ApplicationController

  rescue_from Mailchimp::Exception::DataException,
    Mailchimp::Exception::APIKeyError,
    Mailchimp::Exception::NotFound,
    Mailchimp::Exception::Duplicate,
    Mailchimp::Exception::MissingField,
    Mailchimp::Exception::BadRequest,
    Mailchimp::Exception::UnknownAttribute,
    Mailchimp::Exception::MissingId,
    with: :error

  def error(e)

    puts 'Message: ' + e.message
    puts 'Type: ' + e.type
    puts 'Title: ' + e.title

    e.errors.each do |error|
      puts 'Field: ' + error['field']
      puts 'Message: ' + error['message']
    end if e.errors

    # Respond to the HTTP POST request by passing the errors
    return render_with(500, e.message, e.errors)

  end

  private

  def render_with(status_code, message, errors='none')

    if errors == 'none'
      status = 'success'
      success = true
    else
      status = 'error'
      success = false
    end

    render json: {
      :status => status,
      :success => success,
      :message => message,
      :errors => errors,
      :params => params.as_json
    },
    status: status_code

  end

end

为了让它变干,我已经这样做了......

class MembersController < ApplicationController

  mailchimpExceptions = [
    'DataException',
    'APIKeyError',
    'NotFound',
    'Duplicate',
    'MissingField',
    'BadRequest',
    'UnknownAttribute',
    'MissingId'
  ]

  exceptions = Array.new

  mailchimpExceptions.each do |exception|
    exceptions << "Mailchimp::Exception::#{exception}"
  end

  rescue_from *exceptions, with: :error

  def error(e)

    puts 'Message: ' + e.message
    puts 'Type: ' + e.type
    puts 'Title: ' + e.title

    e.errors.each do |error|
      puts 'Field: ' + error['field']
      puts 'Message: ' + error['message']
    end if e.errors

    # Respond to the HTTP POST request by passing the errors
    return render_with(500, e.message, e.errors)

  end

  private

  def render_with(status_code, message, errors='none')

    if errors == 'none'
      status = 'success'
      success = true
    else
      status = 'error'
      success = false
    end

    render json: {
      :status => status,
      :success => success,
      :message => message,
      :errors => errors,
      :params => params.as_json
    },
    status: status_code

  end

end

我想知道是否所有异常都可以归于一个类,因此只有一个类被称为rescue_from MailchimpExceptions, with: :errorThis answer by mgolubitsky 建议这是可能的,但我不知道如何去做。

我正在使用 gem 'mailchimp_api_v3'。

【问题讨论】:

  • 你用的是什么宝石?
  • @thaleshcv 'mailchimp_api_v3'
  • 是的,这确实是 mailchimp 的疏忽。我认为没有可能的理由 not 为您的 gem 提供一个常见的错误类。也许,你最好的选择是按照@mudasobwa 的建议去做。
  • 也许有的原因,我不知道。请注意,其中大多数 继承自DataException。这样你就可以解救那个和少数几个盗贼了。
  • 现在,RuntimeError 是系统类之一,过于通用。不要救那个一个。

标签: ruby-on-rails ruby ruby-on-rails-5


【解决方案1】:

我对 mailchimp 本身一无所知,但我一般可以建议如何使其正确干燥:

EXCEPTIONS = %w|
  DataException
  APIKeyError
  NotFound
  Duplicate
  MissingField
  BadRequest
  UnknownAttribute
  MissingId|.map { |e| Mailchimp::Exception.const_get(e) }

rescue_from *EXCEPTIONS, with: :error

或者,对于rescue_from 所有例外,立即在Mailchimp::Exception 中定义:

EXCEPTIONS = Mailchimp::Exception.constants.map do |e|
  Mailchimp::Exception.const_get(e)
end.select { |e| e.is_a?(Class) && e < Exception }

【讨论】:

  • 真的很整洁!喜欢它!虽然后者不起作用,但我认为这是因为gem page ArgumentError ({"RestClient::ResourceNotFound"=&gt;Mailchimp::Exception::NotFound, "RestClient::Unauthorized"=&gt;Mailchimp::Exception::APIKeyError} must be an Exception class or a String referencing an Exception class): 出现错误
  • 简单过滤掉所有不例外的东西。我已经更新了答案。
  • 这没有得到任何东西 - 换句话说,它产生了一个空数组
  • 哦,确实。 e 是一个类,不是实例,请查看更新。
  • 现在TypeError (no implicit conversion of Class into Hash):。也许需要将类名作为字符串抓取?
【解决方案2】:

看看https://github.com/dominicsayers/mailchimp_api_v3#exception-handling

上面写着:

所有异常都是 Mailchimp::Exception 的子类

试试rescue_from Mailchimp::Exception, with: :error

【讨论】:

  • 恐怕这不起作用 - 它不能挽救异常。我似乎记得以前尝试过,因为我也在 README.md 中阅读了该行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-23
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
  • 2022-11-29
  • 1970-01-01
  • 2016-05-16
相关资源
最近更新 更多