Commit dfe399b3 authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'sh-catch-pgquery-parse-errors' into 'master'

Gracefully handle SQL parse errors in Sentry error tracker

See merge request gitlab-org/gitlab!76588
parents c07d533e 10af2420
...@@ -142,11 +142,18 @@ module Gitlab ...@@ -142,11 +142,18 @@ module Gitlab
def inject_context_for_exception(event, ex) def inject_context_for_exception(event, ex)
case ex case ex
when ActiveRecord::StatementInvalid 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 else
inject_context_for_exception(event, ex.cause) if ex.cause.present? inject_context_for_exception(event, ex.cause) if ex.cause.present?
end end
end end
def normalize_query(sql)
PgQuery.normalize(sql)
rescue PgQuery::ParseError
sql
end
end end
end end
end end
...@@ -215,6 +215,16 @@ RSpec.describe Gitlab::ErrorTracking do ...@@ -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') expect(sentry_event.dig('extra', 'sql')).to eq('SELECT "users".* FROM "users" WHERE "users"."id" = $2 AND "users"."foo" = $1')
end end
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 end
context 'event processors' do 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