From ee50a5d4bc82cdbf2ed42bf5f1691583207b5347 Mon Sep 17 00:00:00 2001 From: Alara <73926509+alarad27@users.noreply.github.com> Date: Thu, 22 Aug 2024 10:01:06 -0700 Subject: [PATCH] adds error messages for syntax errors (#3486) Adds error messages for syntax errors. --------- Co-authored-by: Jeff Ohrstrom --- apps/dashboard/app/models/announcement.rb | 8 ++++++-- apps/dashboard/test/models/announcements_test.rb | 10 ++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/apps/dashboard/app/models/announcement.rb b/apps/dashboard/app/models/announcement.rb index f2f99d7c35..c348b83894 100644 --- a/apps/dashboard/app/models/announcement.rb +++ b/apps/dashboard/app/models/announcement.rb @@ -88,9 +88,13 @@ def opts {} end @opts.symbolize_keys.compact - rescue Errno::ENOENT, SyntaxError # File does not exist or syntax errors + rescue Errno::ENOENT # File does not exist + Rails.logger.warn "Announcement file not found: #{@path}" @opts = {} - rescue StandardError => e + rescue SyntaxError => e # Syntax errors + Rails.logger.warn "Syntax error in announcement file '#{@path}': #{e.message}. Please check the file for proper syntax." + @opts = {} + rescue StandardError => e # Other exceptions Rails.logger.warn "Error parsing announcement file '#{@path}': #{e.message}" @opts = {} end diff --git a/apps/dashboard/test/models/announcements_test.rb b/apps/dashboard/test/models/announcements_test.rb index 623fa25779..4f83800536 100644 --- a/apps/dashboard/test/models/announcements_test.rb +++ b/apps/dashboard/test/models/announcements_test.rb @@ -79,6 +79,16 @@ class AnnouncementsTest < ActiveSupport::TestCase assert_equal 0, announcements.select(&:valid?).count end + test 'should create announcement for syntax error' do + f = Tempfile.open(['announcement', '.yml']) + f.write('<%- end %>') + f.close + + Rails.logger.expects(:warn).with(regexp_matches(/Syntax error in announcement file/)) + announcement = Announcements.all(f.path).first + assert_equal(:warning, announcement.type) + end + test 'should create invalid announcement for invalid yaml file' do f = Tempfile.open(['announcement', '.yml']) f.write %(INVALID: YAML\nINVALID YAML)