Skip to content

Commit

Permalink
adds error messages for syntax errors (#3486)
Browse files Browse the repository at this point in the history
Adds error messages for syntax errors.

---------

Co-authored-by: Jeff Ohrstrom <[email protected]>
  • Loading branch information
alarad27 and johrstrom authored Aug 22, 2024
1 parent cc26b1c commit ee50a5d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 6 additions & 2 deletions apps/dashboard/app/models/announcement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions apps/dashboard/test/models/announcements_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ee50a5d

Please sign in to comment.