from typing import cast, NamedTuple, Optional, Union
-from ._enums import DiagnosticField, ConnStatus, TransactionStatus
+from ._enums import ConnStatus, TransactionStatus
from .proto import PGconn, PGresult
if hasattr(obj, "error_field"):
# obj is a PGresult
obj = cast(PGresult, obj)
+ bmsg = obj.error_message
- bmsg = obj.error_field(DiagnosticField.MESSAGE_PRIMARY) or b""
- if not bmsg:
- bmsg = obj.error_message
-
- # strip severity and whitespaces
- if bmsg:
- bmsg = bmsg.splitlines()[0].split(b":", 1)[-1].strip()
+ # strip severity and whitespaces
+ if bmsg:
+ bmsg = bmsg.split(b":", 1)[-1].strip()
elif hasattr(obj, "error_message"):
from psycopg3.encodings import py_codecs
# strip severity and whitespaces
if bmsg:
- bmsg = bmsg.splitlines()[0].split(b":", 1)[-1].strip()
+ bmsg = bmsg.split(b":", 1)[-1].strip()
else:
raise TypeError(
res = pgconn.exec_(b"wat")
assert res.status == pq.ExecStatus.FATAL_ERROR
msg = pq.error_message(pgconn)
- assert msg == 'syntax error at or near "wat"'
+ assert msg == 'syntax error at or near "wat"\nLINE 1: wat\n ^'
assert msg == pq.error_message(res)
- assert msg == res.error_field(pq.DiagnosticField.MESSAGE_PRIMARY).decode(
- "ascii"
- )
+ primary = res.error_field(pq.DiagnosticField.MESSAGE_PRIMARY)
+ assert primary.decode("ascii") in msg
with pytest.raises(TypeError):
pq.error_message(None)
await aconn.commit()
assert exc.value.diag.sqlstate == "23503"
+
+
+def test_query_context(conn):
+ with pytest.raises(e.Error) as exc:
+ conn.execute("select * from wat")
+
+ s = str(exc.value)
+ assert "from wat" in s, s
+ assert exc.value.diag.message_primary in s
+ assert "ERROR" not in s
+ assert not s.endswith("\n")