Improve response error message (#1457)
This commit is contained in:
committed by
delvedor
parent
906ba09662
commit
8e638e1cfa
@ -90,7 +90,15 @@ class ResponseError extends ElasticsearchClientError {
|
||||
super('Response Error')
|
||||
Error.captureStackTrace(this, 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
|
||||
}
|
||||
|
||||
@ -108,6 +116,10 @@ class ResponseError extends ElasticsearchClientError {
|
||||
get headers () {
|
||||
return this.meta.headers
|
||||
}
|
||||
|
||||
toString () {
|
||||
return JSON.stringify(this.meta.body)
|
||||
}
|
||||
}
|
||||
|
||||
class RequestAbortedError extends ElasticsearchClientError {
|
||||
|
||||
@ -103,3 +103,75 @@ test('RequestAbortedError', t => {
|
||||
t.true(err.hasOwnProperty('meta'))
|
||||
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()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user