0

So far I have this...

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 

In an attempt to make it DRY, I have done this...

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 

I am wondering if all the exceptions could by under one class, so that only one class is called like rescue_from MailchimpExceptions, with: :error. This answer by mgolubitsky suggests it is possible, but I have no idea how to go about it.

I am using gem 'mailchimp_api_v3'.

8
  • what gem are you using? Commented Mar 15, 2017 at 10:58
  • @thaleshcv 'mailchimp_api_v3' Commented Mar 15, 2017 at 11:01
  • Yeah, it's really an oversight on mailchimp's part. I see no possible reason not to have a common error class for your gem. Probably, your best bet is to do what @mudasobwa is suggesting. Commented Mar 15, 2017 at 11:07
  • 1
    Maybe there was a reason for this, I have no idea. Note that most of them inherit from DataException. So you can rescue that one and the few rogues. Commented Mar 15, 2017 at 11:31
  • 1
    Now, RuntimeError is one of the system classes and is too generic. Don't rescue that one. Commented Mar 15, 2017 at 12:18

2 Answers 2

2

Take a look at https://github.com/dominicsayers/mailchimp_api_v3#exception-handling

It says:

All exceptions will be subclasses of Mailchimp::Exception

try rescue_from Mailchimp::Exception, with: :error

Sign up to request clarification or add additional context in comments.

1 Comment

I'm afraid that doesn't work - It doesn't rescue the exception. I seem to remember trying that before, as I also read that line in the README.md
2

I have no idea about mailchimp itself, but I can generally suggest how to make it DRY properly:

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

Or, to rescue_from all exceptions, defined in Mailchimp::Exception at once:

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

7 Comments

Real tidy! Like it! Though the latter didn't work, I think because of an error on this gem page ArgumentError ({"RestClient::ResourceNotFound"=>Mailchimp::Exception::NotFound, "RestClient::Unauthorized"=>Mailchimp::Exception::APIKeyError} must be an Exception class or a String referencing an Exception class):
Simply filter out everything that is not an exception. I have the answer updated.
That doesn't pick up anything - In other words, it produced an empty array
Oh, indeed. e is a class there, not instance, please see an update.
Now TypeError (no implicit conversion of Class into Hash):. Maybe the Class name needs to be grabbed as a string?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.