finer exception handling
albert <[email protected]> Wed, 19 May 2010 17:55:46 -0400
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <[email protected]> |
--===============7445674345785983831==
Content-Type: multipart/alternative; boundary=005045013e913917ce0486f985c2
--005045013e913917ce0486f985c2
Content-Type: text/plain; charset=ISO-8859-1
Greetings all,
I need to handle an exception test case to distinguish the following results
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:
http://initd.org/psycopg/docs/module.html#exceptions
I can't find a way to classify the error cases mentioned above. The pgerror
and pgcode exception variables arrive as None objects to me whenever I catch
any of the exceptions. I can't rely on the error messages as my application
is multilingual. I am using Ubuntu 9.10 karmic. Code snippet follows...
try:
connection = psycopg2.connect("dbname='%s' user='postgres' password='%s'
host='localhost'" % (dbname,
postgres_password));
cur = connection.cursor()
cur.execute ('select something')
r = cur.fetchone()
except psycopg2.InterfaceError:
print "interface"
return False
except psycopg2.DatabaseError:
print "database"
return False
except psycopg2.OperationalError:
print "operational"
return False
Guidance much appreciated
Albert
--005045013e913917ce0486f985c2
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Greetings all,<br><br>I need to handle an exception test case to distinguis=
h the following results out of a query:<br><br>1. the postgres server is do=
wn<br>2. the login password is wrong<br>
3. the database does not exist<br><br>I have played around catching the psy=
copg2 exceptions as documented here:<br><br>=A0=A0 <a href=3D"http://initd.=
org/psycopg/docs/module.html#exceptions">http://initd.org/psycopg/docs/modu=
le.html#exceptions</a><br>
<br>I can't find a way to classify the error cases mentioned above. The=
pgerror and pgcode exception variables arrive as None objects to me whenev=
er I catch any of the exceptions. I can't rely on the error messages as=
my application is multilingual. I am using Ubuntu 9.10 karmic. Code snippe=
t follows...<br>
<br>try:<br>=A0=A0 connection =3D psycopg2.connect("dbname=3D'%s&#=
39; user=3D'postgres' password=3D'%s' host=3D'localhost=
'" % (dbname, postgres_password));=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0 <br>
=A0=A0 cur =3D connection.cursor()<br>=A0=A0 cur.execute ('select somet=
hing')<br>=A0=A0 r =3D cur.fetchone()<br>except psycopg2.InterfaceError=
:<br>=A0=A0 print "interface"<br>=A0=A0 return False<br>except ps=
ycopg2.DatabaseError:<br>
=A0=A0 print "database"<br>=A0=A0 return False<br>except psycopg2=
.OperationalError:<br>=A0=A0 print "operational"<br>=A0=A0 return=
False<br><br>Guidance much appreciated<br>Albert<br><br>
--005045013e913917ce0486f985c2--
--===============7445674345785983831==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Psycopg mailing list
Psycopg-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/psycopg
--===============7445674345785983831==--