question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Show beautiful GraphQL error messages

See original GitHub issue

GraphQL returns great error messages:

GraphQLError: [{"message":"Resource not accessible by integration","type":"FORBIDDEN","path":["resource","author","hovercard"],"locations":[{"line":11,"column":15}]}]

We should take advantage of that and make them B-E-A-utiful!

There was an error in your GraphQL Query:

on line 11, column 15:
              ... on User {
                hovercard {
                
                ^-- FORBIDDEN: Resource not accessible by integration

We need to get a full spec on what all the GraphQL errors will include, but here’s some thoughts:

  • If locations is included, then show the original query with a pointer to exactly where the error occurred
  • Colorize the output so the error message is in red, and the context is in gray/white
  • If there are multiple errors, do this for all of them

cc @cjoudrey @tcbyrd

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
tcbyrdcommented, May 30, 2018

As discussed in Slack

And also looking back at the spec that was linked above:

If the data entry in the response is not null, the errors entry in the response may contain any errors that occurred during execution. If errors occurred during execution, it should contain those errors.

So per the spec, errors can still be present even when partial data is returned. On the client side (Apollo, Relay), they recommend looking at error types and only presenting ones in the UI that make sense to display to the user. I think it would make sense here to just use log.error for any error messages instead of throwing.

Does GitHub return a different status code if it is able to return a partial response vs not able to respond at all?

Testing this locally, I get GitHub request: POST /graphql - 200 OK for all responses, including queries that return no data and all errors, and ones that return partial data and some errors. This means we can never intentionally throw, since some data can still be returned even though other resources are blocked.

Example:

{
  "data": {
    "resource": {
      "title": "Radar: Weekly Planning for 2018-05-13",
      "createdAt": "2018-05-13T01:00:34Z",
    }
  },
  "errors": [
    {
      "message": "Resource protected by organization SAML enforcement. You must grant your personal token access to this organization.",
      "type": "FORBIDDEN",
      "path": [
        "resource",
        "comments",
        "edges",
        0,
        "node"
      ],
      "locations": [
        {
          "line": 10,
          "column": 11
        }
      ]
    }
  ]
}

Pushing the errors up into the client could be accomplished by only returning res.data (instead of res.data.data), then doing something like this:

const { data, errors } = await context.github.query(getResource, {
      url: context.payload.issue.html_url
    })

if (errors) { context.log.error({ errors }) }

const { resource } = data

It doesn’t feel that awesome, but since the errors have types that need to be handled based on the needs of the app, I think we have to push the errors up into the client.

0reactions
gr2mcommented, Dec 8, 2020

the graphql error messages are much more useful now that we use @octokit/graphql. I made a follow up issue for the format Brandon suggested above: https://github.com/octokit/graphql.js/issues/239

Read more comments on GitHub >

github_iconTop Results From Across the Web

Full Stack Error Handling with GraphQL and Apollo
Having well-defined error codes in your error responses lays the groundwork to make GraphQL errors more actionable and universal. The benefits ...
Read more >
Handling GraphQL errors like a champ with unions and ...
Error handling can be frustrating in GraphQL. This post shows you how to use unions and interfaces to handle errors in a more...
Read more >
A Guide to GraphQL Errors
We love GraphQL for it's type system. The errors key of a GraphQL response, commonly called "Top-Level" errors, don't have such schema. They...
Read more >
The Definitive Guide to Handling GraphQL Errors | by Matt Krick
There are a plethora of errors that a client can encounter when querying a GraphQL Server. Whether it's a query, mutation, or subscription,...
Read more >
200 OK! Error Handling in GraphQL - Sasha Solomon - Medium
You get an error. And you'll find it in the errors array . Canonically, this is where errors can be found in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found