Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CCI] Connection request method callback #478

Merged
merged 3 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message usually provides additional information on why it was thrown. In this case, it just repeats the name of the error. Does the added error message provide any utility?

Copy link
Collaborator Author

@timursaurus timursaurus Apr 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestAbortedError invokes its superclass with message arg:

class RequestAbortedError extends OpenSearchClientError {
  constructor(message, meta) {
    super(message);
    ...
    this.message = message || 'Request aborted';
  }
}

but it invokes it before the default value of message "Request aborted" is set. It's not a big deal, but it raises eslint and typescript errors in the IDE, potentially stopping linting in the future

We also could assign the default value in the constructor

constructor(message = "Request aborted", meta)

Apart from type-safety and better developer experience, it brings no other utility

};

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());
});