From 3ba419c841fb12cd20e6f6c411406616a591f638 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Thu, 20 Apr 2023 13:00:36 -0300 Subject: [PATCH] preserve ClientError cause with es2022 (#22762) Building the client package with `es2022`, exposing the optional `.cause` property on Errors effectively allowing us to wrap caught errors in the client package and re-throw with the context from the request, all while preserving a useful backtrace. This change has potentially material impact to older plugins that attempt to rely on the newer package, but this should only occur at compile time since the webapp doesn't dynamically export this client package. Co-authored-by: Mattermost Build --- webapp/platform/client/src/client4.test.ts | 18 ++++++++++++++++++ webapp/platform/client/src/client4.ts | 6 +++--- webapp/platform/client/tsconfig.json | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/webapp/platform/client/src/client4.test.ts b/webapp/platform/client/src/client4.test.ts index a684b5199a..d6a4eed31d 100644 --- a/webapp/platform/client/src/client4.test.ts +++ b/webapp/platform/client/src/client4.test.ts @@ -68,6 +68,24 @@ describe('ClientError', () => { expect(copy.status_code).toEqual(error.status_code); expect(copy.url).toEqual(error.url); }); + + test('cause should be preserved when provided', () => { + const cause = new Error('the original error'); + const error = new ClientError('https://example.com', { + message: 'This is a message', + server_error_id: 'test.app_error', + status_code: 418, + url: 'https://example.com/api/v4/error', + }, cause); + + const copy = {...error}; + + expect(copy.message).toEqual(error.message); + expect(copy.server_error_id).toEqual(error.server_error_id); + expect(copy.status_code).toEqual(error.status_code); + expect(copy.url).toEqual(error.url); + expect(error.cause).toEqual(cause); + }); }); describe('trackEvent', () => { diff --git a/webapp/platform/client/src/client4.ts b/webapp/platform/client/src/client4.ts index 47e6741b0e..ba9d713775 100644 --- a/webapp/platform/client/src/client4.ts +++ b/webapp/platform/client/src/client4.ts @@ -4168,7 +4168,7 @@ export default class Client4 { throw new ClientError(this.getUrl(), { message: 'Received invalid response from the server.', url, - }); + }, err); } if (headers.has(HEADER_X_VERSION_ID) && !headers.get('Cache-Control')) { @@ -4311,8 +4311,8 @@ export class ClientError extends Error implements ServerError { server_error_id?: string; status_code?: number; - constructor(baseUrl: string, data: ServerError) { - super(data.message + ': ' + cleanUrlForLogging(baseUrl, data.url || '')); + constructor(baseUrl: string, data: ServerError, cause?: any) { + super(data.message + ': ' + cleanUrlForLogging(baseUrl, data.url || ''), {cause}); this.message = data.message; this.url = data.url; diff --git a/webapp/platform/client/tsconfig.json b/webapp/platform/client/tsconfig.json index 992f9814f1..2dad4b02eb 100644 --- a/webapp/platform/client/tsconfig.json +++ b/webapp/platform/client/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "module": "commonjs", "moduleResolution": "node", - "target": "es6", + "target": "es2022", "declaration": true, "strict": true, "resolveJsonModule": true,