pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Vince Gonzalez <vince@baruch.cuny.edu>
To: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] User-defined error messages/codes
Date: Thu, 27 Jan 2000 18:42:54 -0500
Message-ID: <20000127184254.A30658@newton.baruch.cuny.edu> (raw)
In-Reply-To: <20000127181328.E24586@newton.baruch.cuny.edu>
References: <20000127181328.E24586@newton.baruch.cuny.edu>

[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



view thread (2+ messages)

Message-ID: <20000127184254.A30658@newton.baruch.cuny.edu>
Permalink:  ../20000127184254.A30658@newton.baruch.cuny.edu/
Also on:    postgresql.org/message-id/20000127184254.A30658@newton.baruch.cuny.edu

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: vince@baruch.cuny.edu
  Subject: Re: [SQL] User-defined error messages/codes
  In-Reply-To: <20000127184254.A30658@newton.baruch.cuny.edu>
  References: <20000127181328.E24586@newton.baruch.cuny.edu> <20000127184254.A30658@newton.baruch.cuny.edu>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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