diff --git a/CHANGELOG.md b/CHANGELOG.md index c8212a2f0..e769e4654 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -98,6 +98,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Changed - Remove test artifacts from gh_pages workflow ([#335](https://github.com/opensearch-project/opensearch-js/issues/335)) +- Make `TimeoutError` and `RequestAbortedError` classes' `meta` argument optional ([#478](https://github.com/opensearch-project/opensearch-js/pull/478/)) ### Deprecated diff --git a/lib/Connection.js b/lib/Connection.js index bd71d2c53..680b94026 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -134,7 +134,7 @@ class Connection { request.once('error', () => {}); // we need to catch the request aborted error debug('Request aborted', params); this._openRequests--; - callback(new RequestAbortedError(), null); + callback(new RequestAbortedError('Request aborted'), null); }; request.on('response', onResponse); diff --git a/lib/errors.d.ts b/lib/errors.d.ts index 659975167..a907f5110 100644 --- a/lib/errors.d.ts +++ b/lib/errors.d.ts @@ -51,7 +51,7 @@ export declare class ConnectionError< name: string; message: string; meta: ApiResponse; - constructor(message: string, meta: ApiResponse); + constructor(message: string, meta?: ApiResponse); } export declare class NoLivingConnectionsError< @@ -104,7 +104,7 @@ export declare class RequestAbortedError< name: string; message: string; meta: ApiResponse; - constructor(message: string, meta: ApiResponse); + constructor(message: string, meta?: ApiResponse); } export declare class NotCompatibleError< diff --git a/test/unit/connection.test.js b/test/unit/connection.test.js index d9f0268db..3724de112 100644 --- a/test/unit/connection.test.js +++ b/test/unit/connection.test.js @@ -1116,3 +1116,35 @@ test('Abort with a slow body', (t) => { setImmediate(() => request.abort()); }); + +test('Abort with message', (t) => { + t.plan(2); + + const connection = new Connection({ + url: new URL('https://localhost:9200'), + proxy: 'http://localhost:8080', + }); + + const slowBody = new Readable({ + read() { + setTimeout(() => { + this.push('{"size":1, "query":{"match_all":{}}}'); + this.push(null); // EOF + }, 1000).unref(); + }, + }); + + const request = connection.request( + { + method: 'GET', + path: '/', + body: slowBody, + }, + (err) => { + t.ok(err instanceof RequestAbortedError); + t.equal(err.message, 'Request aborted'); + } + ); + + setImmediate(() => request.abort()); +});