Commit ba7eccf7 authored by Luke Duncalfe's avatar Luke Duncalfe

Allow #markup_unsafe to handle StandardError

Our markup renderers are capable of raising errors other than
RuntimeError, for example, the exception raised in
https://gitlab.com/gitlab-org/gitlab/issues/195625 is a TypeError.

This change makes #markup_unsafe catch StandardError.

It also adds tracking for the exception so the error will be visible
in Sentry.
parent 373a352e
......@@ -154,7 +154,9 @@ module MarkupHelper
else
other_markup_unsafe(file_name, text, context)
end
rescue RuntimeError
rescue StandardError => e
Gitlab::ErrorTracking.track_exception(e, project_id: @project&.id, file_name: file_name, context: context)
simple_format(text)
end
......
......@@ -357,10 +357,10 @@ describe MarkupHelper do
describe '#markup_unsafe' do
subject { helper.markup_unsafe(file_name, text, context) }
let_it_be(:project_base) { create(:project, :repository) }
let_it_be(:context) { { project: project_base } }
let(:file_name) { 'foo.bar' }
let(:text) { 'Noël' }
let(:project_base) { build(:project, :repository) }
let(:context) { { project: project_base } }
context 'when text is missing' do
let(:text) { nil }
......@@ -383,12 +383,21 @@ describe MarkupHelper do
context 'when renderer returns an error' do
before do
allow(Banzai).to receive(:render).and_raise("An error")
allow(Banzai).to receive(:render).and_raise(StandardError, "An error")
end
it 'returns html (rendered by ActionView:TextHelper)' do
is_expected.to eq('<p>Noël</p>')
end
it 'logs the error' do
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(
instance_of(StandardError),
project_id: project.id, file_name: 'foo.md', context: context
)
subject
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment