graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

Error handling puts JSON string into error message

Open tettoffensive opened this issue 3 years ago • 5 comments

https://github.com/prisma-labs/graphql-request/blob/777cc55f3f772f5b527df4b7b4ae5f66006b30e9/src/types.ts#L29

This behavior seems really strange to me. I would thing the error message would just contain the error message, not a stringified version of the JSON which is both hard to read and parse. If someone finds this useful maybe it could be under a different key and keep message as is.

tettoffensive avatar Aug 28 '20 18:08 tettoffensive

Agreed, this non-standard approach is difficult to test against and makes user-friendly error messages on the client almost impossible. Is there an alternative plan or approach to offer access to the original error object?

davekiss avatar Dec 26 '20 21:12 davekiss

Hey I just found out some interesting things playing with the error response and trying to extract the error from the response:

typeof error === 'object'

and this brought me to:

JSON.parse(JSON.stringify(error))

which returned me:

{ response: {..}, request: {..} }

And this allowed me to access the error: JSON.parse(JSON.stringify(error)).response.errors[0].message

This is super counterintuitive, but I'm glad I found a cleaner way to get the error message instead of using a regex or manipulating the string itself.

Hope it can help others as well :)

Screenshot 2021-03-19 at 09 52 59

gabrielgatu avatar Mar 19 '21 09:03 gabrielgatu

This is strange behaviour. Was there a specific intention behind this design?

fishactual avatar Jun 05 '21 04:06 fishactual

Just echoing that this behavior seems unintuitive and makes it difficult to parse and display errors. Hoping there is an alternative or "escape hatch" planned.

seans887 avatar Jul 26 '21 14:07 seans887

Any updates on this?

rodgetech avatar Aug 07 '21 03:08 rodgetech

IIUC this issue is made obsolete by https://github.com/jasonkuhrt/graphql-request/issues/509.

jasonkuhrt avatar May 10 '23 22:05 jasonkuhrt