Commit 36dd486c authored by Yorick Peterse's avatar Yorick Peterse

Handle errors without causes

The method `LoadBalancer#connection_error?` would unwrap errors that
inherit from ActiveRecord::StatementInvalid and
ActionView::Template::Error, in an attempt to see if any connection
errors were wrapped. When given an error without a cause, such as
ActiveRecord::RecordNotUnique, it would fail, as `cause` on the error
returns `nil`. This in turn would manifest in various tests, such as
those for the `ApplicationRecord` model.

This commit fixes these issues by just returning `false` when there is
no error cause.

Changelog: fixed
parent 812a57a4
...@@ -169,7 +169,11 @@ module Gitlab ...@@ -169,7 +169,11 @@ module Gitlab
when ActiveRecord::StatementInvalid, ActionView::Template::Error when ActiveRecord::StatementInvalid, ActionView::Template::Error
# After connecting to the DB Rails will wrap query errors using this # After connecting to the DB Rails will wrap query errors using this
# class. # class.
connection_error?(error.cause) if (cause = error.cause)
connection_error?(cause)
else
false
end
when *CONNECTION_ERRORS when *CONNECTION_ERRORS
true true
else else
......
...@@ -283,6 +283,12 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do ...@@ -283,6 +283,12 @@ RSpec.describe Gitlab::Database::LoadBalancing::LoadBalancer, :request_store do
expect(lb.connection_error?(error)).to eq(false) expect(lb.connection_error?(error)).to eq(false)
end end
it 'returns false for ActiveRecord errors without a cause' do
error = ActiveRecord::RecordNotUnique.new
expect(lb.connection_error?(error)).to eq(false)
end
end end
describe '#serialization_failure?' do describe '#serialization_failure?' 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