From 68adb3974e1c738d2074039e06763855480d6f3f Mon Sep 17 00:00:00 2001 From: Mantas Date: Mon, 27 Sep 2021 17:24:09 +0300 Subject: [PATCH] Fixes #3028 - Syntax errors break scheduler job for good --- lib/notification_factory/renderer.rb | 4 ++ .../lib/notification_factory/renderer_spec.rb | 52 ++++++------------- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/lib/notification_factory/renderer.rb b/lib/notification_factory/renderer.rb index b69fad924..669279bd9 100644 --- a/lib/notification_factory/renderer.rb +++ b/lib/notification_factory/renderer.rb @@ -37,6 +37,10 @@ examples how to use def render ERB.new(@template.to_s).result(binding) + rescue Exception => e # rubocop:disable Lint/RescueException + raise StandardError, e.message if e.is_a? SyntaxError + + raise end # d - data of object diff --git a/spec/lib/notification_factory/renderer_spec.rb b/spec/lib/notification_factory/renderer_spec.rb index 87635a90b..1785ba2a0 100644 --- a/spec/lib/notification_factory/renderer_spec.rb +++ b/spec/lib/notification_factory/renderer_spec.rb @@ -19,7 +19,6 @@ RSpec.describe NotificationFactory::Renderer do objects: { ticket: ticket }, template: '#{ticket.customer.firstname.downcase}' expect(renderer.render).to eq 'nicole' - ticket.destroy end it 'correctly renders multiple value calls' do @@ -28,7 +27,21 @@ RSpec.describe NotificationFactory::Renderer do objects: { ticket: ticket }, template: '#{ticket.created_at.value.value.value.value.to_s.first}' expect(renderer.render).to eq '2' - ticket.destroy + end + + it 'raises a StandardError when rendering a template with a broken syntax' do + renderer = build :notification_factory_renderer, template: 'test <% if %>', objects: {} + expect { renderer.render }.to raise_error(StandardError) + end + + it 'raises a StandardError when rendering a template calling a non existant method' do + renderer = build :notification_factory_renderer, template: 'test <% Ticket.non_existant_method %>', objects: {} + expect { renderer.render }.to raise_error(StandardError) + end + + it 'raises a StandardError when rendering a template referencing a non existant object' do + renderer = build :notification_factory_renderer, template: 'test <% NonExistantObject.first %>', objects: {} + expect { renderer.render }.to raise_error(StandardError) end context 'when handling ObjectManager::Attribute usage', db_strategy: :reset do @@ -44,13 +57,6 @@ RSpec.describe NotificationFactory::Renderer do template: '#{ticket.select} _SEPERATOR_ #{ticket.select.value}' expect(renderer.render).to eq 'key_1 _SEPERATOR_ value_1' - ticket.destroy - - ObjectManager::Attribute.remove( - object: 'Ticket', - name: 'select', - ) - ObjectManager::Attribute.migration_execute end it 'correctly renders select attributes on chained user object' do @@ -69,13 +75,6 @@ RSpec.describe NotificationFactory::Renderer do template: '#{ticket.customer.select} _SEPERATOR_ #{ticket.customer.select.value}' expect(renderer.render).to eq 'key_2 _SEPERATOR_ value_2' - ticket.destroy - - ObjectManager::Attribute.remove( - object: 'User', - name: 'select', - ) - ObjectManager::Attribute.migration_execute end it 'correctly renders select attributes on chained group object' do @@ -94,13 +93,6 @@ RSpec.describe NotificationFactory::Renderer do template: '#{ticket.group.select} _SEPERATOR_ #{ticket.group.select.value}' expect(renderer.render).to eq 'key_3 _SEPERATOR_ value_3' - ticket.destroy - - ObjectManager::Attribute.remove( - object: 'Group', - name: 'select', - ) - ObjectManager::Attribute.migration_execute end it 'correctly renders select attributes on chained organization object' do @@ -118,13 +110,6 @@ RSpec.describe NotificationFactory::Renderer do template: '#{ticket.customer.organization.select} _SEPERATOR_ #{ticket.customer.organization.select.value}' expect(renderer.render).to eq 'key_2 _SEPERATOR_ value_2' - ticket.destroy - - ObjectManager::Attribute.remove( - object: 'Organization', - name: 'select', - ) - ObjectManager::Attribute.migration_execute end it 'correctly renders tree select attributes' do @@ -138,13 +123,6 @@ RSpec.describe NotificationFactory::Renderer do template: '#{ticket.tree_select} _SEPERATOR_ #{ticket.tree_select.value}' expect(renderer.render).to eq 'Incident::Hardware::Laptop _SEPERATOR_ Incident::Hardware::Laptop' - ticket.destroy - - ObjectManager::Attribute.remove( - object: 'Ticket', - name: 'tree_select', - ) - ObjectManager::Attribute.migration_execute end end end