facebook / relay

Relay is a JavaScript framework for building data-driven React applications.
https://relay.dev
MIT License
18.41k stars 1.83k forks source link

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

Open renatonmendes opened 7 years ago

renatonmendes commented 7 years ago

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

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

orlowang commented 6 years ago

same situation

claritee commented 6 years ago

Looks to be related to this https://github.com/facebook/relay/issues/1913#issuecomment-330759634

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.