Re: finer exception handling

Daniele Varrazzo <[email protected]> Thu, 20 May 2010 03:00:09 +0100
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
On Wed, May 19, 2010 at 10:55 PM, albert <[email protected]> wrote:
> Greetings all,
>
> I need to handle an exception test case to distinguish the following resu=
lts
> out of a query:
>
> 1. the postgres server is down
> 2. the login password is wrong
> 3. the database does not exist
>
> I have played around catching the psycopg2 exceptions as documented here:
>
> =A0=A0 http://initd.org/psycopg/docs/module.html#exceptions
>
> I can't find a way to classify the error cases mentioned above. The pgerr=
or
> and pgcode exception variables arrive as None objects to me whenever I ca=
tch
> any of the exceptions.

I think that's because the pgcode is populated reading the error on
the connection object, whereas the errors you mention are generated
while creating the connection.

> I can't rely on the error messages as my application
> is multilingual.

The messages are generated by the database in the language set for the
backend, see the lc_messages config parameter. Does your application
really need to publish the error messages from the database to the end
user? If not you can set the backend language to English and use the
messages to detect the error. It is not the most robust thing, but I
don't have any better idea.

-- Daniele