pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
User-defined error messages/codes
2+ messages / 1 participants
[nested] [flat]

* User-defined error messages/codes
@ 2000-01-27 23:13  Vince Gonzalez <vince@baruch.cuny.edu>
  0 siblings, 1 reply; 2+ messages in thread

From: Vince Gonzalez @ 2000-01-27 23:13 UTC (permalink / raw)
  To: pgsql-sql

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




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: [SQL] User-defined error messages/codes
@ 2000-01-27 23:42  Vince Gonzalez <vince@baruch.cuny.edu>
  parent: Vince Gonzalez <vince@baruch.cuny.edu>
  0 siblings, 0 replies; 2+ messages in thread

From: Vince Gonzalez @ 2000-01-27 23:42 UTC (permalink / raw)
  To: pgsql-sql

[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



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2000-01-27 23:42 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-01-27 23:13 User-defined error messages/codes Vince Gonzalez <vince@baruch.cuny.edu>
2000-01-27 23:42 ` Vince Gonzalez <vince@baruch.cuny.edu>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox