args = attrs.field()
ec = attrs.field(default=None)
+ def _explain_encoding_error(self, ex):
+ '''Return a SQLEncodingError instance
+ '''
+ if isinstance(ex, UnicodeEncodeError):
+ return core_ex.SQLEncodingError(
+ ex,
+ ("Data cannot be represented in the database"
+ " connection's client-side character encoding"),
+ (f'The SQL statement is ({self.stmt}) and the data supplied'
+ f' is ({self.tupl})'))
+ if isinstance(ex, psycopg.errors.UntranslatableCharacter):
+ return core_ex.SQLEncodingError(
+ ex,
+ ("Data cannot be represented in the"
+ " character encoding of the database"),
+ (f'The SQL statement is ({self.stmt}) and the data supplied'
+ f' is ({self.tupl})'))
+ return ex
+
def execute(self, cur):
'''
Execute the sql statement.
'''
try:
cur.execute(self.stmt, self.args)
+ except UnicodeEncodeError as ex:
+ if self.ec is None:
+ raise self._explain_encoding_error(ex)
+ raise self.ec(self._explain_encoding_error(ex))
+ except psycopg.errors.UntranslatableCharacter as ex:
+ if self.ec is None:
+ raise self._explain_encoding_error(ex)
+ raise self.ec(self._explain_encoding_error(ex))
except psycopg.DatabaseError as ex:
if self.ec is None:
raise ex
- else:
- raise self.ec(ex)
+ raise self.ec(ex)
@attrs.define(slots=False)
super(DataInconsistencyError, self).__init__(e, descr, detail)
+class SQLEncodingError(SetupError):
+ '''There was an encoding error processing a SQL statement
+ '''
+ def __init__(self, e, descr='', detail=''):
+ super().__init__(e, descr, detail)
+
+
class DBError(SetupError):
'''psycopg3 raised an error'''
def __init__(self, pgexc, e='process your request'):