Graphql-js: Don't swallow all errors in graphql function

Created on 6 Jul 2015  路  20Comments  路  Source: graphql/graphql-js

There seems to be no way to get errors out of graphql function and one only gets error title, which can be the dreaded undefined is not a function deep inside schema or resolve. Would be nice if there'd be a flag to let errors pass through from graphql function, when they are not validation errors. I'd be willing to implement it if this change makes sense.

Thanks!

enhancement

Most helpful comment

Using formatError as well, is there a way to pass the same error handler when using in testing? (in tests you most of the time query directly using the graphql object to execute the query with the schema.

import {
  graphql,
} from 'graphql';
...
let resp = (await graphql(Schema, query, {}, context));

All 20 comments

Yeah this would be useful. Maybe a dev flag which could be passed to the graphql function during dev which exposes stack traces as well as error messages?

Yeah, I feel this pain as well. I have a broader error handling refactor I need to do that should help this be less painful

:+1: Really hard to debug ATM.

+1 on this.

This is killing me.

in error/formatError.js I've added the line:

    stack: error.stack,

to the JSON returned and it has helped tremendously.

+1 for this

Thanks for the tip, @johanatan! Lifesaver!

+1 good call johanastan

Now that #118 has been merged; what's the next step forward here? graphql/express-graphql#4 is closed but to my knowledge this issue is not resolved. How can we help?

+1

Has anybody patched this with https://github.com/graphql/graphql-js/issues/28#issuecomment-128847828 via @johanatan?

+1, also, @johanatan, thanks a lot. I've already made a statue from you

+1

This seems to be fixed here: https://github.com/graphql/express-graphql/pull/45/files

  graphiql: true,
  formatError: (error) => ({
    message: error.message,
    details: config.isProduction ? null : error.stack
  })

Closing this since graphql-js no longer formats Errors anymore (the errors field on the output is a list of the original Error instances now).

Formatting is still recommended when using over a web endpoint, and express-graphql does the simple thing by default and allows for customization.

Using formatError as well, is there a way to pass the same error handler when using in testing? (in tests you most of the time query directly using the graphql object to execute the query with the schema.

import {
  graphql,
} from 'graphql';
...
let resp = (await graphql(Schema, query, {}, context));

I have currently the same problem. I'm not quite sure how to test my custom error messages. Or is it better to test the whole express-graphql endpoint for the error messages?

Agreed, how do we get a stack trace for the errors reported from using the graphql function directly, which I'm also doing in tests and get errors from my code with just the message and no trace.

Just stumbled upon this issue and not quite sure why is it closed. I only get the error title when invoking with graphql or execute...

@bkoltai @alfaproject You can access original errors and their stack traces, like that:

const result = await execute(/* ... */);
if (result.errors) {
  result.errors.forEach(err => console.log(err.originalError));
  // ...
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hekike picture hekike  路  4Comments

davide-ganito picture davide-ganito  路  4Comments

henry74 picture henry74  路  4Comments

adriano-di-giovanni picture adriano-di-giovanni  路  3Comments

sudheerj picture sudheerj  路  3Comments