Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rethrow human-readable error on schema introspection #50

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,27 @@ class GraphqurlCommand extends Command {
queryErrorCb(this, error, queryType, parsedQuery);
};

if (queryString === null) {
queryString = await executeQueryFromTerminalUI({
endpoint: endpoint,
headers,
variables,
name: flags.name,
}, successCallback, errorCallback);
return;
}

const queryOptions = {
query: queryString,
endpoint: endpoint,
endpoint,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you want to do this assignment after you check if queryString is null.
Assigning here and spreading later is just an extra step IMO.

headers,
variables,
name: flags.name,
};

if (queryString === null) {
queryString = await executeQueryFromTerminalUI(queryOptions, successCallback, errorCallback);
return;
}

cli.action.start('Executing query');
await query(queryOptions, successCallback, errorCallback);
await query(
{
query: queryString,
...queryOptions,
},
successCallback,
errorCallback
);
}

parseHeaders(headersArray) {
Expand Down
18 changes: 17 additions & 1 deletion src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,23 @@ const executeQueryFromTerminalUI = async (queryOptions, successCb, errorCb) =>
headers,
} = queryOptions;
cli.action.start('Introspecting schema');
const schemaResponse = await query({endpoint: endpoint, query: introspectionQuery, headers: headers});

const schemaResponse = await query({endpoint: endpoint, query: introspectionQuery, headers: headers})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better readable way for catching errors in the async await pattern is:

let schemaResponse;
try {
  schemaResponse = await query({...})
} catch (e) {
  // throw
}

.catch(err => {
if (err.message && err.message.startsWith('Network error: Unexpected token')) {
Copy link
Contributor

@wawhal wawhal Oct 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BlackDahila you might want to check this condition. I tried running the command:

./bin/run http://localhost:random --introspect

It gave me this error:

Executing query... error
Error:  ApolloError: Network error: request to http://localhost/:random failed, reason: connect ECONNREFUSED 127.0.0.1:80
    at new ApolloError (/home/wawhal/oss/graphql/graphqurl/node_modules/apollo-client/bundle.umd.js:124:32)
    at /home/wawhal/oss/graphql/graphqurl/node_modules/apollo-client/bundle.umd.js:1248:45
    at /home/wawhal/oss/graphql/graphqurl/node_modules/apollo-client/bundle.umd.js:1680:21
    at Array.forEach (<anonymous>)
    at /home/wawhal/oss/graphql/graphqurl/node_modules/apollo-client/bundle.umd.js:1679:22
    at Map.forEach (<anonymous>)
    at QueryManager.broadcastQueries (/home/wawhal/oss/graphql/graphqurl/node_modules/apollo-client/bundle.umd.js:1672:26)
    at /home/wawhal/oss/graphql/graphqurl/node_modules/apollo-client/bundle.umd.js:1175:35
    at processTicksAndRejections (internal/process/task_queues.js:85:5) {
  graphQLErrors: [],
  networkError: FetchError: request to http://localhost/:random failed, reason: connect ECONNREFUSED 127.0.0.1:80
      at ClientRequest.<anonymous> (/home/wawhal/oss/graphql/graphqurl/node_modules/node-fetch/lib/index.js:1345:11)
      at ClientRequest.emit (events.js:203:13)
      at Socket.socketErrorListener (_http_client.js:399:9)
      at Socket.emit (events.js:203:13)
      at emitErrorNT (internal/streams/destroy.js:91:8)
      at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
      at processTicksAndRejections (internal/process/task_queues.js:77:11) {
    message: 'request to http://localhost/:random failed, reason: connect ECONNREFUSED 127.0.0.1:80',
    type: 'system',
    errno: 'ECONNREFUSED',
    code: 'ECONNREFUSED'
  },
  message: 'Network error: request to http://localhost/:random failed, reason: connect ECONNREFUSED 127.0.0.1:80',
  extraInfo: undefined
}

const {networkError} = err;

throw new Error(
`Invalid GraphQL endpoint${
networkError ?
`: [${networkError.statusCode}] ${networkError.response.statusText}` :
''
}`);
}

throw err;
});

cli.action.stop('done');
const r = schemaResponse.data;
// term.fullscreen(true);
Expand Down