Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- from psycopg2.extensions import cursor
- class ErrorThrowingCursor(cursor):
- def __init__(self, conn, *args, **kwargs):
- self.conn = conn
- super(ErrorThrowingCursor, self).__init__(*args, **kwargs)
- def execute(self, query, vars=None):
- result = super(ErrorThrowingCursor, self).execute(query, vars)
- for notice in conn.notices:
- level, message = notice.split(": ")
- if level == "WARNING":
- raise psycopg2.Warning(message.strip())
- return result
- conn = psycopg2.connect(user="user", password="secret")
- cursor = conn.cursor(conn, cursor_factory=ErrorThrowingCursor)
- psycopg2.Warning: there is already a transaction in progress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement