Skip to content

Commit

Permalink
op-reg: Improve error messaging for the operation registry plugin. (#95)
Browse files Browse the repository at this point in the history
* op-reg: Improve error messaging for the operation registry plugin.

* Correct incorrect comment about return value.

As pointed out by @trevor-scheer in https://github.com/apollographql/apollo-platform-commercial/pull/95/files#r281749516
  • Loading branch information
abernix committed May 22, 2019
1 parent 6ad5446 commit 252fc4c
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions packages/apollo-server-plugin-operation-registry/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default class Agent {
await pulse();
} catch (err) {
console.error(
'Apollo Server could not begin serving requests immediately because the operation manifest could not be fetched. Attempts will continue to fetch the manifest, but all requests will be forbidden until the manifest is fetched.',
'The operation manifest could not be fetched. Retries will continue, but requests will be forbidden until the manifest is fetched.',
err.message || err,
);
}
Expand Down Expand Up @@ -162,33 +162,44 @@ export default class Agent {
let response: Response;
try {
response = await fetch(manifestUrl, fetchOptions);
} catch (err) {
const ourErrorPrefix = `Unable to fetch operation manifest for ${
this.options.schemaHash
} in '${this.options.engine.serviceID}': ${err}`;

err.message = `${ourErrorPrefix}: ${err.message}`;
// When the response indicates that the resource hasn't changed, there's
// no need to do any other work. Returning false is meant to indicate
// that there wasn't an update, but there was a successful fetch.
if (response.status === 304) {
this.logger.debug(
'The published manifest was the same as the previous attempt.',
);
return false;
}

throw err;
}
if (!response.ok) {
const responseText = await response.text();

// When the response indicates that the resource hasn't changed, there's
// no need to do any other work. Returning true indicates that this is
// a successful fetch and that we can be assured the manifest is current.
if (response.status === 304) {
this.logger.debug(
'The published manifest was the same as the previous attempt.',
);
return false;
}
// The response error code only comes in XML, but we don't have an XML
// parser handy, so we'll just match the string.
if (responseText.includes('<Code>AccessDenied</Code>')) {
throw new Error(
`No manifest found. Ensure this server's schema has been published with 'apollo service:push' and that operations have been registered with 'apollo client:push'.`,
);
}

if (!response.ok) {
throw new Error(`Could not fetch manifest ${await response.text()}`);
}
// For other unknown errors.
throw new Error(`Unexpected status: ${responseText}`);
}

const contentType = response.headers.get('content-type');
if (contentType && contentType !== 'application/json') {
throw new Error(`Unexpected 'Content-Type' header: ${contentType}`);
}
} catch (err) {
const ourErrorPrefix = `Unable to fetch operation manifest for service '${
this.options.engine.serviceID
}' and schema '${this.options.schemaHash}'. `;

err.message = `${ourErrorPrefix}: ${err}`;

const contentType = response.headers.get('content-type');
if (contentType && contentType !== 'application/json') {
throw new Error(`Unexpected 'Content-Type' header: ${contentType}`);
throw err;
}

await this.updateManifest(await response.json());
Expand Down

0 comments on commit 252fc4c

Please sign in to comment.