Skip to content

Commit

Permalink
rescue Exceptions in dashboard widgets (#3873)
Browse files Browse the repository at this point in the history
  • Loading branch information
johrstrom authored Oct 18, 2024
1 parent 2f2fc65 commit 7bf7941
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
10 changes: 9 additions & 1 deletion apps/dashboard/app/helpers/dashboard_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ def render_widget(widget)
begin
render partial: "widgets/#{widget}"
rescue SyntaxError, StandardError => e
render partial: 'shared/widget_error', locals: { error: e, widget: widget.to_s }
render_error_widget(e, widget.to_s)
# rubocop:disable Lint/RescueException - because these can throw all sorts of errors.
rescue Exception => e
# rubocop:enable Lint/RescueException
render_error_widget(e, widget.to_s)
end
end

private

def render_error_widget(error, widget_name)
render(partial: 'shared/widget_error', locals: { error: error, widget: widget_name })
end

def default_dashboard_layout
if xdmod?
if pinned_apps? || motd?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%- require 'the_missing_gem' -%>
15 changes: 10 additions & 5 deletions apps/dashboard/test/integration/dashboard_layout_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,16 @@ def test_env
{
columns: [
{
width: 6,
width: 4,
widgets: 'this_widget_doesnt_exist'
},
{
width: 6,
width: 4,
widgets: 'syntax_error'
},
{
width: 4,
widgets: 'load_error'
}
]
}
Expand All @@ -263,12 +267,13 @@ def test_env
end

assert_select 'div.row', 1
assert_select 'div.row > div.col-md-6', 2
assert_select 'div.row > div.col-md-4', 3

error_widgets = css_select('div.row > div.col-md-6 > div.alert.alert-danger.card > div.card-body')
assert_equal 2, error_widgets.size
error_widgets = css_select('div.row > div.col-md-4 > div.alert.alert-danger.card > div.card-body')
assert_equal 3, error_widgets.size
assert_equal true, %r{Missing partial widgets/_this_widget_doesnt_exist}.match?(error_widgets[0].text)
assert_equal true, /undefined method `woops!'/.match?(error_widgets[1].text)
assert_equal true, /cannot load such file -- the_missing_gem/.match?(error_widgets[2].text)
end

test 'should render brand new widgets with shipped widgets' do
Expand Down

0 comments on commit 7bf7941

Please sign in to comment.