diff --git a/apps/dashboard/app/models/announcement.rb b/apps/dashboard/app/models/announcement.rb index e9a6b41b98..a91680aac6 100644 --- a/apps/dashboard/app/models/announcement.rb +++ b/apps/dashboard/app/models/announcement.rb @@ -49,9 +49,15 @@ 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 + @error = e.message + Rails.logger.warn "Syntax error in announcement file '#{@path}': #{e.message}. Please check the file for proper syntax." + @opts = {} + rescue => e # Other exceptions + @error = e.message 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 30fb2a10f4..389a974dd2 100644 --- a/apps/dashboard/test/models/announcements_test.rb +++ b/apps/dashboard/test/models/announcements_test.rb @@ -77,6 +77,17 @@ class AnnouncementsTest < ActiveSupport::TestCase assert_equal 0, announcements.select(&:valid?).count end + test "should create announcement for syntax error" do + f = Tempfile.open(["announcemment", ".yml"]) + f.write "<%- end %>" + f.close + + announcements = Announcements.all(f.path) + announcement = announcements.first + assert_equal :warning, announcement.type + assert_send([announcement.instance_variable_get(:@error).to_str, :include?, "syntax error"]) + end + test "should create invalid announcement for invalid yaml file" do f = Tempfile.open(["announcement", ".yml"]) f.write %{INVALID: YAML\nINVALID YAML}