Skip to content

Commit

Permalink
feat: providing a way to disable message content being logged (#1786) -
Browse files Browse the repository at this point in the history
fixes #1751
  • Loading branch information
Parama92 authored May 14, 2024
1 parent aea11d0 commit b98ef1e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
50 changes: 49 additions & 1 deletion packages/web-api/src/WebClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require('mocha');
const fs = require('fs');
const path = require('path');
const { Agent } = require('https');
const { assert } = require('chai');
const { assert, expect } = require('chai');
const { WebClient, buildThreadTsWarningMessage } = require('./WebClient');
const { ErrorCode } = require('./errors');
const { LogLevel } = require('./logger');
Expand Down Expand Up @@ -1612,6 +1612,54 @@ describe('WebClient', function () {
});
});

describe('has an option to suppress request error from Axios', () => {

beforeEach(function () {
this.scope = nock('https://slack.com')
.post(/api/)
.replyWithError('Request failed!!')
})

it('the \'original\' property is attached when the option, attachOriginalToWebAPIRequestError is absent', async () => {
const client = new WebClient(token, {
retryConfig: { retries: 0 },
});

client.apiCall('conversations/list').catch((error) => {
expect(error).to.haveOwnProperty('original')
this.scope.done();
done();
});

});

it('the \'original\' property is attached when the option, attachOriginalToWebAPIRequestError is set to true', async () => {
const client = new WebClient(token, {
attachOriginalToWebAPIRequestError: true,
retryConfig: { retries: 0 },
});

client.apiCall('conversations/list').catch((error) => {
expect(error).to.haveOwnProperty('original')
this.scope.done();
done();
});
});

it('the \'original\' property is not attached when the option, attachOriginalToWebAPIRequestError is set to false', async () => {
const client = new WebClient(token, {
attachOriginalToWebAPIRequestError: false,
retryConfig: { retries: 0 },
});

client.apiCall('conversations/list').catch((error) => {
expect(error).not.to.haveOwnProperty('original')
this.scope.done();
done();
});
});
});

afterEach(function () {
nock.cleanAll();
});
Expand Down
17 changes: 16 additions & 1 deletion packages/web-api/src/WebClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ export interface WebClientOptions {
rejectRateLimitedCalls?: boolean;
headers?: Record<string, string>;
teamId?: string;
/**
* Indicates whether to attach the original error to a Web API request error.
* When set to true, the original error object will be attached to the Web API request error.
* @type {boolean}
* @default true
*/
attachOriginalToWebAPIRequestError?: boolean,
}

export type TLSOptions = Pick<SecureContextOptions, 'pfx' | 'key' | 'passphrase' | 'cert' | 'ca'>;
Expand Down Expand Up @@ -167,6 +174,12 @@ export class WebClient extends Methods {
*/
private teamId?: string;

/**
* Configuration to opt-out of attaching the original error
* (obtained from the HTTP client) to WebAPIRequestError.
*/
private attachOriginalToWebAPIRequestError: boolean;

/**
* @param token - An API token to authenticate/authorize with Slack (usually start with `xoxp`, `xoxb`)
*/
Expand All @@ -182,6 +195,7 @@ export class WebClient extends Methods {
rejectRateLimitedCalls = false,
headers = {},
teamId = undefined,
attachOriginalToWebAPIRequestError = true,
}: WebClientOptions = {}) {
super();

Expand All @@ -195,6 +209,7 @@ export class WebClient extends Methods {
this.tlsConfig = tls !== undefined ? tls : {};
this.rejectRateLimitedCalls = rejectRateLimitedCalls;
this.teamId = teamId;
this.attachOriginalToWebAPIRequestError = attachOriginalToWebAPIRequestError;

// Logging
if (typeof logger !== 'undefined') {
Expand Down Expand Up @@ -613,7 +628,7 @@ export class WebClient extends Methods {
const e = error as any;
this.logger.warn('http request failed', e.message);
if (e.request) {
throw requestErrorWithOriginal(e);
throw requestErrorWithOriginal(e, this.attachOriginalToWebAPIRequestError);
}
throw error;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/web-api/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@ export function errorWithCode(error: Error, code: ErrorCode): CodedError {
/**
* A factory to create WebAPIRequestError objects
* @param original - original error
* @param attachOriginal - config indicating if 'original' property should be added on the error object
*/
export function requestErrorWithOriginal(original: Error): WebAPIRequestError {
export function requestErrorWithOriginal(original: Error, attachOriginal: boolean): WebAPIRequestError {
const error = errorWithCode(
new Error(`A request error occurred: ${original.message}`),
ErrorCode.RequestError,
) as Partial<WebAPIRequestError>;
error.original = original;
if (attachOriginal) {
error.original = original;
}
return (error as WebAPIRequestError);
}

Expand Down

0 comments on commit b98ef1e

Please sign in to comment.