Postgraphile: functional error interface

Created on 20 Mar 2018  路  7Comments  路  Source: graphile/postgraphile

I'm submitting a ...

  • [ ] bug report
  • [x] feature request
  • [ ] question

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:

  • [ ] access to req because it may have information, for example i18n info, that you could use format text differently
  • [ ] access all errors as an array, which gives ability to reduce
  • [ ] not sure if handleErrors is the best name, but maybe others have better ideas

Most helpful comment

This shipped in v4.0.0-beta.4

All 7 comments

Seems 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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

WestleyArgentum picture WestleyArgentum  路  3Comments

marshall007 picture marshall007  路  3Comments

outsidenote picture outsidenote  路  4Comments

srghma picture srghma  路  3Comments

mrbarletta picture mrbarletta  路  5Comments