Updated abort behavior (#1141)

* Updated abort behavior

- Support for aborting a request with the promise api
- Aborting a request will cause a RequestAbortedError
- Normalized Connection class errors, now every error returned is
wrapped by the client errors constructors

* Updated test

* Updated docs

* Updated code generation script

* Renamed test

* Code coverage

* Avoid calling twice transport.request
This commit is contained in:
Tomas Della Vedova
2020-04-06 11:21:19 +02:00
committed by GitHub
parent 953a8033ab
commit 27a8e2a9bf
16 changed files with 824 additions and 648 deletions

View File

@ -67,45 +67,34 @@ client.search({
=== Aborting a request
When using the callback style API, the function also returns an object that
allows you to abort the API request.
If needed, you can abort a running request by calling the `request.abort()` method returned by the API.
CAUTION: If you abort a request, the request will fail with a `RequestAbortedError`.
[source,js]
----
// calback API
const request = client.search({
index: 'my-index',
body: { foo: 'bar' }
}, {
ignore: [404],
maxRetries: 3
}, (err, { body }) => {
if (err) console.log(err)
}, (err, result) => {
if (err) {
console.log(err) // RequestAbortedError
} else {
console.log(result)
}
})
request.abort()
----
Aborting a request with the promise style API is not supported, but you can
achieve that with convenience wrapper.
The same behavior is valid for the promise style API as well.
[source,js]
----
function abortableRequest (params, options) {
var request = null
const promise = new Promise((resolve, reject) => {
request = client.search(params, options, (err, result) => {
err ? reject(err) : resolve(res)
})
})
return {
promise,
abort: () => request.abort()
}
}
const request = abortableRequest({
const request = client.search({
index: 'my-index',
body: { foo: 'bar' }
}, {
@ -113,8 +102,11 @@ const request = abortableRequest({
maxRetries: 3
})
request
.then(result => console.log(result))
.catch(err => console.log(err)) // RequestAbortedError
request.abort()
// access the promise with `request.promise.[method]`
----
@ -213,6 +205,9 @@ You can find the errors exported by the client in the table below.
|`ConnectionError`
|Generated when an error occurs during the request, it can be a connection error or a malformed stream of data.
|`RequestAbortedError`
|Generated if the user calls the `request.abort()` method.
|`NoLivingConnectionsError`
|Given the configuration, the ConnectionPool was not able to find a usable Connection for this request.