Re: Dynamic use of RAISE with USING to generate and catch non-hardcoded custom exceptions

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: mike davis <mike(dot)davis65(at)hotmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Dynamic use of RAISE with USING to generate and catch non-hardcoded custom exceptions
Date: 2017-09-22 04:54:07
Message-ID: 29032.1506056047@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

mike davis <mike(dot)davis65(at)hotmail(dot)com> writes:
> I'm trying to get dynamic version of the RAISE command working so
> that I can use a table of custom application error messages and codes
> for use by all developed plpgsql functions.

This works for me:

DO $$
DECLARE
v_msg TEXT := 'SOMETHING IS WRONG';
v_sqlstate TEXT := 'E0001';
BEGIN
RAISE EXCEPTION USING message = v_msg, errcode = v_sqlstate;
EXCEPTION
WHEN SQLSTATE 'E0001' THEN
RAISE NOTICE '%','Error E0001 raised - going to do something about it';
WHEN OTHERS THEN
RAISE NOTICE 'OTHER ERRORS: %,%', sqlstate,sqlerrm;
END$$;

NOTICE: Error E0001 raised - going to do something about it

Or you could do

RAISE EXCEPTION SQLSTATE v_sqlstate USING message = v_msg;

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Pavel Stehule 2017-09-22 05:09:50 Re: Dynamic use of RAISE with USING to generate and catch non-hardcoded custom exceptions
Previous Message Pavel Stehule 2017-09-22 04:15:56 Re: Dynamic use of RAISE with USING to generate and catch non-hardcoded custom exceptions