Skip to content

Commit

Permalink
Improve DB::Exception error handling (#165)
Browse files Browse the repository at this point in the history
When the returned row contains `DB::Exception`, the adapter will
erroneously raise an `ActiveRecord::ActiveRecordError`.

The current check for `DB::Exception` doesn't account for that.

Fixes #164
  • Loading branch information
kyrylo authored Oct 23, 2024
1 parent b76c783 commit 41c952f
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module Clickhouse
module SchemaStatements
DEFAULT_RESPONSE_FORMAT = 'JSONCompactEachRowWithNamesAndTypes'.freeze

DB_EXCEPTION_REGEXP = /\ACode: \d.+\. DB::Exception:/.freeze

def execute(sql, name = nil, settings: {})
do_execute(sql, name, settings: settings)
end
Expand Down Expand Up @@ -183,7 +185,9 @@ def apply_format(sql, format)
def process_response(res, format, sql = nil)
case res.code.to_i
when 200
if res.body.to_s.include?("DB::Exception")
body = res.body

if body.include?("DB::Exception") && body.match?(DB_EXCEPTION_REGEXP)
raise ActiveRecord::ActiveRecordError, "Response code: #{res.code}:\n#{res.body}#{sql ? "\nQuery: #{sql}" : ''}"
else
format_body_response(res.body, format)
Expand Down

0 comments on commit 41c952f

Please sign in to comment.