Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/web-api/src/WebClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ describe('WebClient', function () {
r.catch((error) => {
assert.instanceOf(error, Error);
this.scope.done();
assert.match(error.stack, /Error: thrown by\n\s+at WebClient.apiCall/);
done();
});
});
Expand Down
79 changes: 43 additions & 36 deletions packages/web-api/src/WebClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,50 +145,57 @@ export class WebClient extends Methods {
* @param options - options
*/
public async apiCall(method: string, options?: WebAPICallOptions): Promise<WebAPICallResult> {
this.logger.debug(`apiCall('${method}') start`);
const {stack: originalStack} = new Error('thrown by');

warnDeprecations(method, this.logger);
try {
this.logger.debug(`apiCall('${method}') start`);

if (typeof options === 'string' || typeof options === 'number' || typeof options === 'boolean') {
throw new TypeError(`Expected an options argument but instead received a ${typeof options}`);
}
warnDeprecations(method, this.logger);

const response = await this.makeRequest(method, Object.assign(
{ token: this.token },
options,
));
const result = this.buildResult(response);
if (typeof options === 'string' || typeof options === 'number' || typeof options === 'boolean') {
throw new TypeError(`Expected an options argument but instead received a ${typeof options}`);
}

// log warnings in response metadata
if (result.response_metadata !== undefined && result.response_metadata.warnings !== undefined) {
result.response_metadata.warnings.forEach(this.logger.warn.bind(this.logger));
}
const response = await this.makeRequest(method, Object.assign(
{ token: this.token },
options,
));
const result = this.buildResult(response);

// log warnings and errors in response metadata messages
// related to https://api.slack.com/changelog/2016-09-28-response-metadata-is-on-the-way
if (result.response_metadata !== undefined && result.response_metadata.messages !== undefined) {
result.response_metadata.messages.forEach((msg) => {
const errReg: RegExp = /\[ERROR\](.*)/;
const warnReg: RegExp = /\[WARN\](.*)/;
if (errReg.test(msg)) {
const errMatch = msg.match(errReg);
if (errMatch != null) {
this.logger.error(errMatch[1].trim());
}
} else if (warnReg.test(msg)) {
const warnMatch = msg.match(warnReg);
if (warnMatch != null) {
this.logger.warn(warnMatch[1].trim());
// log warnings in response metadata
if (result.response_metadata !== undefined && result.response_metadata.warnings !== undefined) {
result.response_metadata.warnings.forEach(this.logger.warn.bind(this.logger));
}

// log warnings and errors in response metadata messages
// related to https://api.slack.com/changelog/2016-09-28-response-metadata-is-on-the-way
if (result.response_metadata !== undefined && result.response_metadata.messages !== undefined) {
result.response_metadata.messages.forEach((msg) => {
const errReg: RegExp = /\[ERROR\](.*)/;
const warnReg: RegExp = /\[WARN\](.*)/;
if (errReg.test(msg)) {
const errMatch = msg.match(errReg);
if (errMatch != null) {
this.logger.error(errMatch[1].trim());
}
} else if (warnReg.test(msg)) {
const warnMatch = msg.match(warnReg);
if (warnMatch != null) {
this.logger.warn(warnMatch[1].trim());
}
}
}
});
}
});
}

if (!result.ok) {
throw platformErrorFromResult(result as (WebAPICallResult & { error: string; }));
}
if (!result.ok) {
throw platformErrorFromResult(result as (WebAPICallResult & { error: string; }));
}

return result;
return result;
} catch (err) {
err.stack += `\n${originalStack}`;
Copy link
Author

Choose a reason for hiding this comment

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

This currently looks as follows:

Error: An API error occurred: account_inactive
     at Object.platformErrorFromResult (/project/node_modules/@slack/web-api/src/errors.ts:94:5)
     at WebClient.apiCall (/project/node_modules/@slack/web-api/src/WebClient.ts:188:13)
     at runMicrotasks (<anonymous>)
     at processTicksAndRejections (internal/process/task_queues.js:97:5)
Error: thrown by
    at WebClient.apiCall (/project/node_modules/@slack/web-api/dist/WebClient.js:120:40)
    at /project/my_file.ts:288:19

Happy to tweak as needed.

throw err;
}
}

/**
Expand Down