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

catch errors on platform #573

Merged
merged 3 commits into from Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/WebClient.spec.js
Expand Up @@ -191,6 +191,20 @@ describe('WebClient', function () {
});
});

it('should fail with platform errors when the API response is an error', function () {
const scope = nock('https://slack.com')
.post(/api/)
.reply(200, { ok: false, error: 'bad error' });
const client = new WebClient(token);
return client.apiCall('method')
.catch((error) => {
assert.propertyVal(error, 'code', 'slackclient_platform_error');
assert.nestedPropertyVal(error, 'data.ok', false);
assert.nestedPropertyVal(error, 'data.error', 'bad error');
scope.done();
});
});

it('should properly serialize simple API arguments', function () {
const scope = nock('https://slack.com')
// NOTE: this could create false negatives if the serialization order changes (it shouldn't matter)
Expand Down
11 changes: 11 additions & 0 deletions src/WebClient.ts
Expand Up @@ -150,6 +150,16 @@ export class WebClient extends EventEmitter {
if (result.response_metadata !== undefined && result.response_metadata.warnings !== undefined) {
result.response_metadata.warnings.forEach(this.logger.warn);
}

if (!result.ok) {
const error = errorWithCode(
new Error(`An API error occurred: ${result.error}`),
ErrorCode.PlatformError,
);
error.data = result;
throw new pRetry.AbortError(error);
}

return result;
})
.catch((error: got.GotError) => {
Expand All @@ -168,6 +178,7 @@ export class WebClient extends EventEmitter {
// wait and return the result from calling `task` again after the specified number of seconds
return delay(retryAfterMs).then(task);
}

throw httpErrorWithOriginal(error);
} else {
throw error;
Expand Down