Module: PgSaurus::ConnectionAdapters::PostgreSQLAdapter::TranslateException

Included in:
PgSaurus::ConnectionAdapters::PostgreSQLAdapter
Defined in:
lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception.rb

Overview

Extend ActiveRecord::ConnectionAdapter::PostgreSQLAdapter logic to wrap more pg-specific errors into specific exception classes

Constant Summary collapse

INSUFFICIENT_PRIVILEGE =
"42501"

Instance Method Summary collapse

Instance Method Details

#translate_exception(exception, message:, sql:, binds:) ⇒ Object

Intercept insufficient privilege PG::Error and raise active_record wrapped database exception



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/translate_exception.rb', line 8

def translate_exception(exception, message:, sql:, binds:)
  return exception unless exception.respond_to?(:result)
  exception_result = exception.result

  case exception_result.try(:error_field, PG::Result::PG_DIAG_SQLSTATE)
  when INSUFFICIENT_PRIVILEGE
    exc_message = exception_result.try(:error_field, PG::Result::PG_DIAG_MESSAGE_PRIMARY)
    exc_message ||= message
    ::ActiveRecord::InsufficientPrivilege.new(exc_message, sql: sql, binds: binds)
  else
    super
  end
end