Re: Why doesn't `RAISE EXCEPTION` provide error context? - Mailing list pgsql-general

From Alban Hertroys
Subject Re: Why doesn't `RAISE EXCEPTION` provide error context?
Date
Msg-id CAF-3MvN-w_E3u5D6iiipXRPuUO5XMu58++xpG6VffjiZJWhk2A@mail.gmail.com
Whole thread Raw
In response to Re: Why doesn't `RAISE EXCEPTION` provide error context?  (Taylor Brown <taylor@youneedabudget.com>)
List pgsql-general
On 2 April 2015 at 19:15, Taylor Brown <taylor@youneedabudget.com> wrote:
So, I would rather put a check like this at the top of my function:

--
important_variable = (p_request::json->>'important_variable')::integer;
IF (important_variable is NULL) THEN RAISE EXCEPTION 'important_variable must not be NULL.'; END IF;
--

But I won't be able to get the context for that exception, and all I'll be able to return from the function or write to the logs is 'important_variable must not be NULL.'. If that's the only place I throw that error, I'll know where to look. Otherwise, I have no context, and won't be able to determine where my exception was thrown. So I'm actually better off _not_ throwing my own custom exceptions, even though I would prefer to be more defensive about this sort of thing in my code.

I thought I might be able to "trick" postgres into throwing another unrelated exception that would not only include my custom error message, but allow me to extract the context, telling me the function where I should begin debugging.

Just a quick idea, but... Perhaps you could put a constraint on those JSON messages indirectly, by mapping it to some kind of template table?
The top-level of a JSON message can usually be mapped to a table, and a table can have constraints and, for example, a BEFORE INSERT trigger that always returns NULL so that the data is not actually inserted into the table.

--
If you can't see the forest for the trees,
Cut the trees and you'll see there is no forest.

pgsql-general by date:

Previous
From: Octavi Fors
Date:
Subject: Re: The case of PostgreSQL on NFS Server (II)
Next
From: Pavel Stehule
Date:
Subject: Re: