facebook / facebook/relay

[Modern]How to catch the `errors` fields from GraphQL payload when data field is not null.

Open
#2,172 3 comments 7 reactions 0 assignees View on GitHub
wontfix
Dominant language
Rust
Stars
19k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

I came in a situation that another user has inserted invalid data on my database. When queried, I got the following payload from my GraphQL server:

```
{
"errors": [
{
"message": "Cannot return null for non-nullable field Company.multiSite.",
"locations": [
{
"line": 7,
"column": 7
}
],
"stack": "Error: Cannot return null for non-nullable field Company.multiSite.\n at completeValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:596:13)\n at completeValueWithLocatedError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:543:21)\n at completeValueCatchingError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:512:12)\n at resolveField (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:464:10)\n at C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:311:18\n at Array.reduce (native)\n at executeFields (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:308:42)\n at collectAndExecuteSubfields (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:746:10)\n at completeObjectValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:728:10)\n at completeValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:625:12)\n at completeValueWithLocatedError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:543:21)\n at completeValueCatchingError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:518:21)\n at C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:648:25\n at Array.forEach (native)\n at forEach (C:\\DEV\\myproject\\node_modules\\iterall\\index.js:336:25)\n at completeListValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:644:24)",
"path": [
"viewer",
"companies",
112,
"multiSite"
]
},
{
"message": "Cannot return null for non-nullable field Company.multiSite.",
"locations": [
{
"line": 7,
"column": 7
}
],
"stack": "Error: Cannot return null for non-nullable field Company.multiSite.\n at completeValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:596:13)\n at completeValueWithLocatedError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:543:21)\n at completeValueCatchingError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:512:12)\n at resolveField (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:464:10)\n at C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:311:18\n at Array.reduce (native)\n at executeFields (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:308:42)\n at collectAndExecuteSubfields (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:746:10)\n at completeObjectValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:728:10)\n at completeValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:625:12)\n at completeValueWithLocatedError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:543:21)\n at completeValueCatchingError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:518:21)\n at C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:648:25\n at Array.forEach (native)\n at forEach (C:\\DEV\\myproject\\node_modules\\iterall\\index.js:336:25)\n at completeListValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:644:24)",
"path": [
"viewer",
"companies",
113,
"multiSite"
]
},
{
"message": "Cannot return null for non-nullable field Company.multiSite.",
"locations": [
{
"line": 7,
"column": 7
}
],
"stack": "Error: Cannot return null for non-nullable field Company.multiSite.\n at completeValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:596:13)\n at completeValueWithLocatedError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:543:21)\n at completeValueCatchingError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:512:12)\n at resolveField (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:464:10)\n at C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:311:18\n at Array.reduce (native)\n at executeFields (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:308:42)\n at collectAndExecuteSubfields (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:746:10)\n at completeObjectValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:728:10)\n at completeValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:625:12)\n at completeValueWithLocatedError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:543:21)\n at completeValueCatchingError (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:518:21)\n at C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:648:25\n at Array.forEach (native)\n at forEach (C:\\DEV\\myproject\\node_modules\\iterall\\index.js:336:25)\n at completeListValue (C:\\DEV\\myproject\\node_modules\\graphql\\execution\\execute.js:644:24)",
"path": [
"viewer",
"companies",
114,
"multiSite"
]
}
],
"data": {
"viewer": {
"companies": [
{
"id": "Company:59b2cd9a072e4f28b839aaa0",
"name": "Test Company 1",
"ein": "1261063599",
"multiSite": true,
"siteLimit": 10
},
... Dozens of line...
}
}

```

On `QueryRenderer` nothing is set on `error` (that comes `null`). As far as I know this is because `data` is set to non null (has data), and in that case `QueryRenderer` does not return data. (A wirld thing or a bug for me, check [https://github.com/facebook/relay/issues/1913](url)

How can I catch the given error so that my client redirect to a system error page with this detailed information ?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.