From 474a42473adf9b18417242f1fc0691a857ec578b Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 28 Feb 2011 18:41:10 +0200 Subject: PL/Python custom SPI exceptions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This provides a separate exception class for each error code that the backend defines, as well as the ability to get the SQLSTATE from the exception object. Jan UrbaƄski, reviewed by Steve Singer --- src/pl/plpython/sql/plpython_error.sql | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/pl/plpython/sql/plpython_error.sql') diff --git a/src/pl/plpython/sql/plpython_error.sql b/src/pl/plpython/sql/plpython_error.sql index 7861cd61a2..0f456f4bc3 100644 --- a/src/pl/plpython/sql/plpython_error.sql +++ b/src/pl/plpython/sql/plpython_error.sql @@ -131,6 +131,27 @@ return None SELECT valid_type('rick'); +/* check catching specific types of exceptions + */ +CREATE TABLE specific ( + i integer PRIMARY KEY +); + +CREATE FUNCTION specific_exception(i integer) RETURNS void AS +$$ +from plpy import spiexceptions +try: + plpy.execute("insert into specific values (%s)" % (i or "NULL")); +except spiexceptions.NotNullViolation, e: + plpy.notice("Violated the NOT NULL constraint, sqlstate %s" % e.sqlstate) +except spiexceptions.UniqueViolation, e: + plpy.notice("Violated the UNIQUE constraint, sqlstate %s" % e.sqlstate) +$$ LANGUAGE plpythonu; + +SELECT specific_exception(2); +SELECT specific_exception(NULL); +SELECT specific_exception(2); + /* manually starting subtransactions - a bad idea */ CREATE FUNCTION manual_subxact() RETURNS void AS $$ -- cgit v1.2.1