From b98ef1e1fc03483ca030f3e25044d3c610d887bc Mon Sep 17 00:00:00 2001 From: Parama Date: Tue, 14 May 2024 21:50:22 +0530 Subject: [PATCH] feat: providing a way to disable message content being logged (#1786) - fixes #1751 --- packages/web-api/src/WebClient.spec.js | 50 +++++++++++++++++++++++++- packages/web-api/src/WebClient.ts | 17 ++++++++- packages/web-api/src/errors.ts | 7 ++-- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/packages/web-api/src/WebClient.spec.js b/packages/web-api/src/WebClient.spec.js index d74ba7670..40ff34729 100644 --- a/packages/web-api/src/WebClient.spec.js +++ b/packages/web-api/src/WebClient.spec.js @@ -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'); @@ -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(); }); diff --git a/packages/web-api/src/WebClient.ts b/packages/web-api/src/WebClient.ts index 36319ebbb..a5904e180 100644 --- a/packages/web-api/src/WebClient.ts +++ b/packages/web-api/src/WebClient.ts @@ -70,6 +70,13 @@ export interface WebClientOptions { rejectRateLimitedCalls?: boolean; headers?: Record; 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; @@ -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`) */ @@ -182,6 +195,7 @@ export class WebClient extends Methods { rejectRateLimitedCalls = false, headers = {}, teamId = undefined, + attachOriginalToWebAPIRequestError = true, }: WebClientOptions = {}) { super(); @@ -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') { @@ -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; } diff --git a/packages/web-api/src/errors.ts b/packages/web-api/src/errors.ts index 9e8e7b720..efec40292 100644 --- a/packages/web-api/src/errors.ts +++ b/packages/web-api/src/errors.ts @@ -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; - error.original = original; + if (attachOriginal) { + error.original = original; + } return (error as WebAPIRequestError); }