Skip to content

Raise a psycopg error with a specific pgcode #1777

Answered by dvarrazzo
arjenzorgdoc asked this question in Q&A

You must be logged in to vote

Then maybe it's not possible because in psycopg2 database errors are only expected to be raised from C and the attribute is read-only from Python.

In psycopg 3 what you want to do is not a problem:

>>> try: raise psycopg.errors.lookup("40001")()
... except Exception as ex: e = ex

>>> e.sqlstate
'40001'

Replies: 1 comment 3 replies

You must be logged in to vote
3 replies
@arjenzorgdoc

@arjenzorgdoc

@dvarrazzo

Answer selected by arjenzorgdoc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants