From d77e73bb12562f30b1bf93f89f27cf996682e6fc Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Tue, 30 May 2017 12:04:10 +0200 Subject: [PATCH] Zendesk import: Improved exception text for ObjectManager Attribute errors and removed dependency of init_callback implementation. --- lib/import/zendesk/object_attribute.rb | 4 +++- .../lib/import/zendesk/object_attribute_spec.rb | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/import/zendesk/object_attribute.rb b/lib/import/zendesk/object_attribute.rb index 4c8824487..ae8a7fd01 100644 --- a/lib/import/zendesk/object_attribute.rb +++ b/lib/import/zendesk/object_attribute.rb @@ -13,12 +13,14 @@ module Import private def init_callback(_attribute) - raise 'Missing init_callback method implementation for this object attribute' end def add(object, name, attribute) ObjectManager::Attribute.add( attribute_config(object, name, attribute) ) ObjectManager::Attribute.migration_execute(false) + rescue => e + # rubocop:disable Style/SpecialGlobalVars + raise $!, "Problem with ObjectManager Attribute '#{name}': #{$!}", $!.backtrace end def attribute_config(object, name, attribute) diff --git a/spec/lib/import/zendesk/object_attribute_spec.rb b/spec/lib/import/zendesk/object_attribute_spec.rb index 4fb38161d..0539fa263 100644 --- a/spec/lib/import/zendesk/object_attribute_spec.rb +++ b/spec/lib/import/zendesk/object_attribute_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' RSpec.describe Import::Zendesk::ObjectAttribute do - it 'throws an exception if no init_callback is implemented' do + it 'extends ObjectManager Attribute exception text' do attribute = double( title: 'Example attribute', @@ -16,6 +16,19 @@ RSpec.describe Import::Zendesk::ObjectAttribute do type: 'input', ) - expect { described_class.new('Ticket', 'example_field', attribute) }.to raise_error(RuntimeError) + error_text = 'some error' + expect(ObjectManager::Attribute).to receive(:add).and_raise(RuntimeError, error_text) + + exception = nil + begin + described_class.new('Ticket', 'example_field', attribute) + rescue => e + exception = e + end + + expect(exception).not_to be nil + expect(exception).to be_a(RuntimeError) + expect(exception.message).to include(error_text) + expect(exception.message).not_to eq(error_text) end end