Consistency for the win

- The result object contains also the metadata about the request
- The events emits the same object of the API response
- The errors, where possible, exposes the APi response object under the
meta key
This commit is contained in:
delvedor
2019-03-11 17:13:02 +01:00
parent c990ed43d4
commit ae028b2ae2
2 changed files with 53 additions and 32 deletions

View File

@ -27,31 +27,32 @@ class ElasticsearchClientError extends Error {
}
class TimeoutError extends ElasticsearchClientError {
constructor (message, request) {
constructor (message, meta) {
super(message)
Error.captureStackTrace(this, TimeoutError)
this.name = 'TimeoutError'
this.message = message || 'Timeout Error'
this.request = request
this.meta = meta
}
}
class ConnectionError extends ElasticsearchClientError {
constructor (message, request) {
constructor (message, meta) {
super(message)
Error.captureStackTrace(this, ConnectionError)
this.name = 'ConnectionError'
this.message = message || 'Connection Error'
this.request = request
this.meta = meta
}
}
class NoLivingConnectionsError extends ElasticsearchClientError {
constructor (message) {
constructor (message, meta) {
super(message)
Error.captureStackTrace(this, NoLivingConnectionsError)
this.name = 'NoLivingConnectionsError'
this.message = message || 'No Living Connections Error'
this.meta = meta
}
}
@ -83,16 +84,27 @@ class ConfigurationError extends ElasticsearchClientError {
}
class ResponseError extends ElasticsearchClientError {
constructor ({ body, statusCode, headers }) {
constructor (meta) {
super('Response Error')
Error.captureStackTrace(this, ResponseError)
this.name = 'ResponseError'
this.message = (body && body.error && body.error.type) || 'Response Error'
this.body = body
this.statusCode = body && typeof body.status === 'number'
? body.status
: statusCode
this.headers = headers
this.message = (meta.body && meta.body.error && meta.body.error.type) || 'Response Error'
this.meta = meta
}
get body () {
return this.meta.body
}
get statusCode () {
if (this.meta.body && typeof this.meta.body.status === 'number') {
return this.meta.body.status
}
return this.meta.statusCode
}
get headers () {
return this.meta.headers
}
}