Skip to content

Commit

Permalink
[CCI] Connection request method callback (#478)
Browse files Browse the repository at this point in the history
* fix: callback error required args

Signed-off-by: Timur Bolotov <[email protected]>
  • Loading branch information
timursaurus authored Apr 10, 2023
1 parent 20e9282 commit 553d1a8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/errors.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export declare class ConnectionError<
name: string;
message: string;
meta: ApiResponse<TResponse, TContext>;
constructor(message: string, meta: ApiResponse);
constructor(message: string, meta?: ApiResponse);
}

export declare class NoLivingConnectionsError<
Expand Down Expand Up @@ -104,7 +104,7 @@ export declare class RequestAbortedError<
name: string;
message: string;
meta: ApiResponse<TResponse, TContext>;
constructor(message: string, meta: ApiResponse);
constructor(message: string, meta?: ApiResponse);
}

export declare class NotCompatibleError<
Expand Down
32 changes: 32 additions & 0 deletions test/unit/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});

0 comments on commit 553d1a8

Please sign in to comment.