Skip to content

Commit

Permalink
adds error messages for syntax errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alarad27 committed Mar 26, 2024
1 parent c356c5c commit 97d33f9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions apps/dashboard/app/models/announcement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions apps/dashboard/test/models/announcements_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 97d33f9

Please sign in to comment.