Advertisement
Guest User

Untitled

a guest
Jul 6th, 2016
2,348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. import psycopg2
  2. from psycopg2.extensions import cursor
  3.  
  4. class ErrorThrowingCursor(cursor):
  5. def __init__(self, conn, *args, **kwargs):
  6. self.conn = conn
  7. super(ErrorThrowingCursor, self).__init__(*args, **kwargs)
  8.  
  9. def execute(self, query, vars=None):
  10. result = super(ErrorThrowingCursor, self).execute(query, vars)
  11.  
  12. for notice in conn.notices:
  13. level, message = notice.split(": ")
  14. if level == "WARNING":
  15. raise psycopg2.Warning(message.strip())
  16.  
  17. return result
  18.  
  19. conn = psycopg2.connect(user="user", password="secret")
  20. cursor = conn.cursor(conn, cursor_factory=ErrorThrowingCursor)
  21.  
  22. psycopg2.Warning: there is already a transaction in progress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement