Commit 10af2420 authored by Stan Hu's avatar Stan Hu

Gracefully handle SQL parse errors in Sentry error tracker

If a SQL query cannot be parsed, previously the Sentry error tracker
would throw a `PgQuery::ParseError` instead of handling it gracefully.
This commit fixes a spec failure that occurred when a bad SQL query gets
caught by the cross-join detector and bubbles up to the Sentry error
tracker.

Closes https://gitlab.com/gitlab-org/gitlab/-/issues/348207
parent 4a0ca8f0
......@@ -142,11 +142,18 @@ module Gitlab
def inject_context_for_exception(event, ex)
case ex
when ActiveRecord::StatementInvalid
event.extra[:sql] = PgQuery.normalize(ex.sql.to_s)
# StatementInvalid may be caused by a statement timeout or a bad query
event.extra[:sql] = normalize_query(ex.sql.to_s)
else
inject_context_for_exception(event, ex.cause) if ex.cause.present?
end
end
def normalize_query(sql)
PgQuery.normalize(sql)
rescue PgQuery::ParseError
sql
end
end
end
end
......@@ -215,6 +215,16 @@ RSpec.describe Gitlab::ErrorTracking do
expect(sentry_event.dig('extra', 'sql')).to eq('SELECT "users".* FROM "users" WHERE "users"."id" = $2 AND "users"."foo" = $1')
end
end
context 'when the `ActiveRecord::StatementInvalid` is a bad query' do
it 'injects the query as-is into extra' do
allow(exception).to receive(:cause).and_return(ActiveRecord::StatementInvalid.new(sql: 'SELECT SELECT FROM SELECT'))
track_exception
expect(sentry_event.dig('extra', 'sql')).to eq('SELECT SELECT FROM SELECT')
end
end
end
context 'event processors' do
......
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