- pl/pgSQL - How can I intercept an eventual exception and put it in a text ? - Mailing list pgsql-general

From Bruno Baguette
Subject - pl/pgSQL - How can I intercept an eventual exception and put it in a text ?
Date
Msg-id !~!UENERkVCMDkAAQACAAAAAAAAAAAAAAAAABgAAAAAAAAATBuXKOMvlkWzD3KJN6FWLMKAAAAQAAAA9NF9Wvbgz0m6V6WcrOnuoAEAAAAA@netcourrier.com
Whole thread Raw
List pgsql-general
Hello,

Here's a pl/pgSQL function that simply add a two values inside a table.

CREATE function tuple_add(VARCHAR(200)) RETURNS text AS
'
DECLARE
Response text;
PeopleName ALIAS FOR $1;
BEGIN
INSERT INTO PEOPLES VALUES (nextval(''sequence_id''),PeopleName);
return Response;
END;
'   LANGUAGE 'plpgsql';

If the user try to add the same value (for the PeopleName field),
PostGreSQL will throw an exception and print "ERROR: Cannot insert a
duplicate key into unique index biblio_maisonsediti_libelle_key ".

How can I intercept ANY exception inside the function, and to put in the
"Response" variable the text of the EXCEPTION ?

In other words, I want to avoid to have my function to throw any
exception, so I want to intercept an eventual exception and to take the
text ("ERROR: Cannot insert a duplicate key into unique index
biblio_maisonsediti_libelle_key" for example) and to put it in the
"Response" variable so the program that call my function will receive
the error message.

Thanks in advance for your help !  :-)

-------------------------------------------------
Bruno BAGUETTE - bruno.baguette@netcourrier.com


pgsql-general by date:

Previous
From: Don Isgitt
Date:
Subject: db design help needed
Next
From: Tom Lane
Date:
Subject: Re: Optimizing suggestions