Skip to content

Instantly share code, notes, and snippets.

@coderholic
Created October 14, 2011 13:05
Show Gist options
  • Save coderholic/1287038 to your computer and use it in GitHub Desktop.
Save coderholic/1287038 to your computer and use it in GitHub Desktop.
>>> from django.db import connection
>>> connection.cursor().execute("SELECT nothing FROM non_existant_table")
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "...lib/python2.6/site-packages/django/db/backends/util.py", line 34, in execute
return self.cursor.execute(sql, params)
File "...lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "...lib/python2.6/site-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "...lib/python2.6/site-packages/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
DatabaseError: (1146, "Table 'database.non_existant_table' doesn't exist")
>>> def errorhandler(originalhandler):
... def _errorhandler(cursor, errorclass, errorvalue):
... if cursor and cursor._last_executed:
... args = list(errorvalue.args)
... args.append(cursor._last_executed)
... errorvalue = errorclass(*args)
... return originalhandler(cursor, errorclass, errorvalue)
... return _errorhandler
...
>>> originalhandler = connection.connection.errorhandler
>>> connection.connection.errorhandler = errorhandler(originalhandler)
>>>
>>> connection.cursor().execute("SELECT nothing FROM non_existant_table")
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "...lib/python2.6/site-packages/django/db/backends/util.py", line 34, in execute
return self.cursor.execute(sql, params)
File "...lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "...lib/python2.6/site-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "<input>", line 7, in _errorhandler
File "...lib/python2.6/site-packages/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
DatabaseError: (1146, "Table 'database.non_existant_table' doesn't exist", 'SELECT nothing FROM non_existant_table')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment