I'm submitting a ...
I'm thinking of adding an entry point to simply modify the error objects to give the client more specific or formatted messages.
To keep it functional, I believe something like this where you literally just map() over the errors would be ideal. If somebody wanted to, they could theoretically reduce() as well, as the return value is an Array of these errors:
app.use(
postgraphile(DATABASE_URL, SCHEMAS.split(','), {
graphiql: true,
pgSettings: (req: Request) => {
const user_id =
req.session && req.session.passport && req.session.passport.user;
return {
'user.id': user_id ? user_id : undefined,
role: user_id ? 'authenticated' : 'anonymous',
};
},
handleErrors: (errors: GraphQLFormattedErrorExtended[], req: Request, res: Response) => {
return errors.map(error=>{
return i18nCustomErrorFunction(req, error);
});
},
})
);
some notes:
handleErrors is the best name, but maybe others have better ideasSeems that the implementation would involve createPostGraphileHttpRequestHandler.js
@benjie I started to dig around and not sure which would be the one to catch them all:
Looks like a place to wrap errors here: https://github.com/graphile/postgraphile/blob/master/src/postgraphile/http/createPostGraphileHttpRequestHandler.js#L139
I also found these locations where errors are managed as well:
this one looks like validation errors: https://github.com/graphile/postgraphile/blob/master/src/postgraphile/http/createPostGraphileHttpRequestHandler.js#L483
this one looks more generic: https://github.com/graphile/postgraphile/blob/master/src/postgraphile/http/createPostGraphileHttpRequestHandler.js#L534
this looks like a place we're already doing some formatting: https://github.com/graphile/postgraphile/blob/master/src/postgraphile/http/createPostGraphileHttpRequestHandler.js#L545
First thoughts: 馃憤
Please change argument order : handleErrors(errors, req). We might want to change to handleErrors(errors, req, res) so you can set an error code or a response header of some kind.
updated! I like the idea of the response, good call!
That last one is where I would have done it - it鈥檚 where we already do it with the other error options. You should throw an error if the other error options are set also.
OK, I have a first attempt: https://github.com/graphile/postgraphile/pull/723/files
I'm gonna try and test it first, I'm just making the PR so we can see the changes and get feedback
I don't understand why we'd change any of the flow and throw errors. I just tested and things work as expected without modifying very much. The current PR is very non-invasive ;)
also I added a check for req.headersSent and it's a part of the node core: https://nodejs.org/api/http.html#http_response_headerssent in case developers send their own response.
This shipped in v4.0.0-beta.4
Most helpful comment
This shipped in v4.0.0-beta.4