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 <build@mattermost.com>
Этот коммит содержится в:
Jesse Hallam
2023-04-20 13:00:36 -03:00
коммит произвёл GitHub
родитель a22ae500e5
Коммит 3ba419c841
3 изменённых файлов: 22 добавлений и 4 удалений

Просмотреть файл

@@ -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', () => {

Просмотреть файл

@@ -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;