Improve response error message (#1457) (#1461)

Co-authored-by: Tomas Della Vedova <delvedor@users.noreply.github.com>
This commit is contained in:
github-actions[bot]
2021-05-03 16:39:54 +02:00
committed by GitHub
parent b812c51dce
commit c9808fb067
2 changed files with 85 additions and 1 deletions

View File

@ -90,7 +90,15 @@ class ResponseError extends ElasticsearchClientError {
super('Response Error') super('Response Error')
Error.captureStackTrace(this, ResponseError) Error.captureStackTrace(this, ResponseError)
this.name = 'ResponseError' this.name = 'ResponseError'
this.message = (meta.body && meta.body.error && meta.body.error.type) || 'Response Error' if (meta.body && meta.body.error && meta.body.status) {
if (Array.isArray(meta.body.error.root_cause)) {
this.message = meta.body.error.root_cause.map(entry => `[${entry.type}] Reason: ${entry.reason}`).join('; ')
} else {
this.message = 'Response Error'
}
} else {
this.message = 'Response Error'
}
this.meta = meta this.meta = meta
} }
@ -108,6 +116,10 @@ class ResponseError extends ElasticsearchClientError {
get headers () { get headers () {
return this.meta.headers return this.meta.headers
} }
toString () {
return JSON.stringify(this.meta.body)
}
} }
class RequestAbortedError extends ElasticsearchClientError { class RequestAbortedError extends ElasticsearchClientError {

View File

@ -103,3 +103,75 @@ test('RequestAbortedError', t => {
t.true(err.hasOwnProperty('meta')) t.true(err.hasOwnProperty('meta'))
t.end() t.end()
}) })
test('ResponseError with meaningful message / 1', t => {
const meta = {
body: {
error: {
root_cause: [
{
type: 'index_not_found_exception',
reason: 'no such index [foo]',
'resource.type': 'index_expression',
'resource.id': 'foo',
index_uuid: '_na_',
index: 'foo'
}
],
type: 'index_not_found_exception',
reason: 'no such index [foo]',
'resource.type': 'index_expression',
'resource.id': 'foo',
index_uuid: '_na_',
index: 'foo'
},
status: 404
},
statusCode: 404,
headers: {}
}
const err = new errors.ResponseError(meta)
t.strictEqual(err.message, '[index_not_found_exception] Reason: no such index [foo]')
t.strictEqual(err.toString(), JSON.stringify(meta.body))
t.end()
})
test('ResponseError with meaningful message / 2', t => {
const meta = {
body: {
error: {
root_cause: [
{
type: 'index_not_found_exception',
reason: 'no such index [foo]',
'resource.type': 'index_expression',
'resource.id': 'foo',
index_uuid: '_na_',
index: 'foo'
},
{
type: 'nested_cause',
reason: 'this is a nested cause',
'resource.type': 'index_expression',
'resource.id': 'foo',
index_uuid: '_na_',
index: 'foo'
}
],
type: 'index_not_found_exception',
reason: 'no such index [foo]',
'resource.type': 'index_expression',
'resource.id': 'foo',
index_uuid: '_na_',
index: 'foo'
},
status: 404
},
statusCode: 404,
headers: {}
}
const err = new errors.ResponseError(meta)
t.strictEqual(err.message, '[index_not_found_exception] Reason: no such index [foo]; [nested_cause] Reason: this is a nested cause')
t.strictEqual(err.toString(), JSON.stringify(meta.body))
t.end()
})