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'.
RuntimeErroris one of the system classes and is too generic. Don't rescue that one.