diff --git a/asyncpg/exceptions/_base.py b/asyncpg/exceptions/_base.py index 00e9699a..92cc7655 100644 --- a/asyncpg/exceptions/_base.py +++ b/asyncpg/exceptions/_base.py @@ -111,12 +111,19 @@ def _make_constructor(cls, fields, query=None): # so we raise a custom subclass that is easier to handle # and identify. # - # Note that we specifically do not rely on the error - # message, as it is localizable. + # PostgreSQL's message is localizable, so its routine + # name identifies the error. CockroachDB raises it from + # routines with other, version-dependent names, but does + # not localize the message, so the untranslated message + # identifies it as well. is_icse = ( exccls.__name__ == 'FeatureNotSupportedError' and _is_asyncpg_class(exccls) and - dct.get('server_source_function') == 'RevalidateCachedQuery' + ( + dct.get('server_source_function') == + 'RevalidateCachedQuery' or + message == 'cached plan must not change result type' + ) ) if is_icse: diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 64ca921d..2d041343 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -54,3 +54,38 @@ async def test_exceptions_str(self): self.assertIn('DETAIL: Function', str(e)) else: self.fail('InvalidFunctionDefinitionError not raised') + + +class TestInvalidCachedStatementClassification(tb.TestCase): + + def _new_error(self, **fields): + return asyncpg.PostgresError.new( + {'C': '0A000', 'S': 'ERROR', **fields}) + + def test_postgres_routine(self): + e = self._new_error( + M='cached plan must not change result type', + R='RevalidateCachedQuery') + self.assertIsInstance(e, asyncpg.InvalidCachedStatementError) + + def test_postgres_routine_translated_message(self): + e = self._new_error( + M='', + R='RevalidateCachedQuery') + self.assertIsInstance(e, asyncpg.InvalidCachedStatementError) + + def test_cockroachdb_routines(self): + # CockroachDB names the Go function that raised the error. + for routine in ('runExecBuilder', 'execBind'): + with self.subTest(routine=routine): + e = self._new_error( + M='cached plan must not change result type', + R=routine) + self.assertIsInstance( + e, asyncpg.InvalidCachedStatementError) + self.assertEqual(e.sqlstate, '0A000') + + def test_other_feature_not_supported(self): + e = self._new_error( + M='unimplemented: this syntax', R='runExecBuilder') + self.assertIs(type(e), asyncpg.FeatureNotSupportedError)