Re: Why doesn't `RAISE EXCEPTION` provide error context?

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: Taylor Brown <taylor(at)youneedabudget(dot)com>
Cc: Melvin Davidson <melvin6925(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Why doesn't `RAISE EXCEPTION` provide error context?
Date: 2015-04-03 07:45:37
Message-ID: CAF-3MvN-w_E3u5D6iiipXRPuUO5XMu58++xpG6VffjiZJWhk2A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2 April 2015 at 19:15, Taylor Brown <taylor(at)youneedabudget(dot)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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ramesh T 2015-04-03 09:27:36
Previous Message Octavi Fors 2015-04-03 06:40:20 Re: The case of PostgreSQL on NFS Server (II)