Open steve-chavez opened 1 year ago
Maybe https://datatracker.ietf.org/doc/rfc7807/ is relevant?
Hm, how so? RFC 7807 is for the error message format right? We discussed adopting that on https://github.com/PostgREST/postgrest/pull/1926#issuecomment-916591592
Just to kickstart discussion. We could have a pre-request function that does:
I don't understand how you could know at the time the pre-request function runs what kind of error to return? Doesn't this error just happen afterwards?
What about:
SAVEPOINT
set before the main query.db-error-handler
function with all those error details as arguments, which returns a "proper" error.On https://github.com/PostgREST/postgrest/issues/3084#issue-2022449376, @dvv mentioned:
I wonder if we could have a post-request db hook in addition to pre-request one? The rationale is to sanitize errors or to reconcile just in-place. It could be given all available info from get stacked diagnostics and raise more meaningful messages or just rewrite the json data.
Hm, I don't think we can use get stacked diagnostics since that would imply plpgsql
?
@steve-chavez
Right, catching the exception directly implies plpgsql wrapping by begin/exception/end
which is not suitable.
But we could honor user-defined optional
create or replace function app_priv.on_response(result json = null, error json = null, statement text = null, out response json) as $$
-- process the successful result: eg. mangle field names, put to the cache for later quick fetch in app.authenticate() etc.
-- or process the error: look into hint/detail and deduce a friendlier error message, log the error at own will etc.
-- or just craft entirely new response: conditionally fail while debugging, whatever.
$$ stable ...;
and pass it the result, the entire SQL.ServerError
if any, the statement if applicable.
Problem
Having:
A failed POST request will return:
Which is not user friendly. We've been telling users to format the error message on the client side, but this is not possible with direct consumers of the API(unless a proxy is involved, which complicates the setup).
Proposal
Just to kickstart discussion. We could have a
pre-request
function that does:Related: https://github.com/PostgREST/postgrest/issues/1438