look into upgrading exception formatting to better separate wrapped exception from statement

Issue #3172 resolved
Tomas Machalek
created an issue

In a special case (described below) when trying to update a sqlite3 database file located in a directory I have no write permissions to, I obtain quite a misleading error message:

unable to open database file "insert into ......."

I.e. it looks like Sqlalchemy thinks the SQL is in fact a path to the database.

How to replicate

(tested on Linux OS)

Manually (i.e. not using a Python code) create a sqlite3 database, e.g. /home/you/data/test.db with some table in it:

CREATE TABLE cache (key text primary key, value text);

Remove your write permissions from the data directory (i.e. not the file itself!).

Run the following code:

from sqlalchemy import create_engine
en = create_engine('sqlite:////home/you/data/test.db')
en.execute("INSERT INTO cache (key, value) VALUES ('foo', 'bar')")

Comments (3)

  1. Michael Bayer repo owner

    possible patch:

    diff --git a/lib/sqlalchemy/exc.py b/lib/sqlalchemy/exc.py
    index a82bae3..e4d6c0a 100644
    --- a/lib/sqlalchemy/exc.py
    +++ b/lib/sqlalchemy/exc.py
    @@ -244,7 +244,8 @@ class StatementError(SQLAlchemyError):
                 "(%s)" % det for det in self.detail
                 ] + [
    -                repr(self.statement), repr(params_repr)
    +                "(original statement:", repr(self.statement),
    +                repr(params_repr), ")"
         def __unicode__(self):
  2. Michael Bayer repo owner
    • Exception messages have been spiffed up a bit. The SQL statement and parameters are not displayed if None, reducing confusion for error messages that weren't related to a statement. The full module and classname for the DBAPI-level exception is displayed, making it clear that this is a wrapped DBAPI exception. The statement and parameters themselves are bounded within a bracketed sections to better isolate them from the error message and from each other. fixes #3172

    → <<cset 6f40eb37cbdc>>

  3. Log in to comment