In case of a GraphQL error, error.message is set to a combined message describing all errors returned by the endpoint.
All errors can be accessed at error.errors. error.request has the request options such as query, variables and headers set for easier debugging.
let{ graphql, GraphqlResponseError }=require("@octokit/graphql");graphql=graphql.defaults({headers: {authorization: `token secret123`,},});constquery=`{ viewer { bioHtml }}`;try{constresult=awaitgraphql(query);}catch(error){if(errorinstanceofGraphqlResponseError){// do something with the error, allowing you to detect a graphql response error,// compared to accidentally catching unrelated errors.// server responds with an object like the following (as an example)// class GraphqlResponseError {// "headers": {// "status": "403",// },// "data": null,// "errors": [{// "message": "Field 'bioHtml' doesn't exist on type 'User'",// "locations": [{// "line": 3,// "column": 5// }]// }]// }console.log("Request failed:",error.request);// { query, variables: {}, headers: { authorization: 'token secret123' } }console.log(error.message);// Field 'bioHtml' doesn't exist on type 'User'}else{// handle non-GraphQL error}}
Partial responses
A GraphQL query may respond with partial data accompanied by errors. In this case we will throw an error but the partial data will still be accessible through error.data
let{ graphql }=require("@octokit/graphql");graphql=graphql.defaults({headers: {authorization: `token secret123`,},});constquery=`{ repository(name: "probot", owner: "probot") { name ref(qualifiedName: "master") { target { ... on Commit { history(first: 25, after: "invalid cursor") { nodes { message } } } } } }}`;try{constresult=awaitgraphql(query);}catch(error){// server responds with// {// "data": {// "repository": {// "name": "probot",// "ref": null// }// },// "errors": [// {// "type": "INVALID_CURSOR_ARGUMENTS",// "path": [// "repository",// "ref",// "target",// "history"// ],// "locations": [// {// "line": 7,// "column": 11// }// ],// "message": "`invalid cursor` does not appear to be a valid cursor."// }// ]// }console.log("Request failed:",error.request);// { query, variables: {}, headers: { authorization: 'token secret123' } }console.log(error.message);// `invalid cursor` does not appear to be a valid cursor.console.log(error.data);// { repository: { name: 'probot', ref: null } }}
请发表评论