Thread: User-defined error messages/codes

User-defined error messages/codes

From
Vince Gonzalez
Date:
Hello,

Is it possible to create user-defined error messages in functions?
For example, I'd like for an update to a table to fail with a message like
'You need to check your privileges' as opposed to 'Permission Denied'.    
I'd like to avoid parsing the messages actually returned by the server,   
as these could change from revision to revision.  Should I do this by     
crafting each function to return an error code that I define, and then    
use that code to index a table?  I'm interested to hear any solutions     
people may have come up with.                                             

I seem to remember there being some sort of exception facility, but I
can't remember where I may have seen it (or I could be crazy).       

My application is written in Java, if that matters.

--vince



Re: [SQL] User-defined error messages/codes

From
Vince Gonzalez
Date:
[Yes, I know it's bad style to reply to one's own posts.  Oh well.]

On Thu, Jan 27, 2000 at 06:13:28PM -0500, Vince Gonzalez wrote:
> Is it possible to create user-defined error messages in functions?
> For example, I'd like for an update to a table to fail with a message like
> 'You need to check your privileges' as opposed to 'Permission Denied'.    
> I'd like to avoid parsing the messages actually returned by the server,   
> as these could change from revision to revision.  Should I do this by     
> crafting each function to return an error code that I define, and then    
> use that code to index a table?  I'm interested to hear any solutions     
> people may have come up with.                                             
> 
> I seem to remember there being some sort of exception facility, but I
> can't remember where I may have seen it (or I could be crazy).       

Man, its funny how posting a question to a mailing list makes it easier
to find the answer on your own. :)  After re-scanning the programmer's
manual, I found info on using RAISE to throw messages:

test=> CREATE TABLE "test" ("a" int4);
CREATE
test=> 
test=> CREATE FUNCTION "throwit" ( ) RETURNS int4 AS '
test'> DECLARE
test'>         arecord RECORD;
test'> BEGIN
test'>         select a into arecord from test where a = 5;
test'>         if not found then
test'>         raise exception ''five not found'';
test'>         end if;
test'>         return 5;
test'> END;
test'> ' LANGUAGE 'plpgsql';
CREATE
test=> 
test=> INSERT INTO test (a) values (1);
INSERT 2168921 1
test=> INSERT INTO test (a) values (2);
INSERT 2168922 1
test=> INSERT INTO test (a) values (3);
INSERT 2168923 1
test=> INSERT INTO test (a) values (4);
INSERT 2168924 1
test=> select throwit();
ERROR:  five not found
test=>

Lovely.  Now, I still am not sure how to proceed with my first question,
of how to handle user privileges in a way that does not require me to actually
attempt an operation to find out if it will succeed.

--vince