[Backport 7.x] Show the body as string if the response error can't be read as ES error (#1514)
* Show the body as string if the response error can't be read as ES error * Updated test Co-authored-by: Tomas Della Vedova <delvedor@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
a48ebc9442
commit
559acd32be
@ -97,8 +97,10 @@ class ResponseError extends ElasticsearchClientError {
|
||||
} else {
|
||||
this.message = meta.body.error.type
|
||||
}
|
||||
} else if (typeof meta.body === 'object' && meta.body != null) {
|
||||
this.message = JSON.stringify(meta.body)
|
||||
} else {
|
||||
this.message = 'Response Error'
|
||||
this.message = meta.body || 'Response Error'
|
||||
}
|
||||
this.meta = meta
|
||||
}
|
||||
|
||||
@ -649,7 +649,7 @@ test('500 error', t => {
|
||||
}
|
||||
}
|
||||
}, (err, result) => {
|
||||
t.equal(err.message, 'Response Error')
|
||||
t.equal(err.message, '{"error":"kaboom"}')
|
||||
|
||||
client.search({
|
||||
index: 'foo',
|
||||
|
||||
@ -25,7 +25,8 @@ const buffer = require('buffer')
|
||||
const intoStream = require('into-stream')
|
||||
const { ConnectionPool, Transport, Connection, errors } = require('../../index')
|
||||
const { CloudConnectionPool } = require('../../lib/pool')
|
||||
const { Client, buildServer } = require('../utils')
|
||||
const { Client, buildServer, connection } = require('../utils')
|
||||
const { buildMockConnection } = connection
|
||||
|
||||
let clientVersion = require('../../package.json').version
|
||||
if (clientVersion.includes('-')) {
|
||||
@ -1584,3 +1585,31 @@ test('caFingerprint can\'t be configured over http / 3', t => {
|
||||
t.fail('shuld not throw')
|
||||
}
|
||||
})
|
||||
|
||||
test('Error body that is not a json', t => {
|
||||
t.plan(5)
|
||||
|
||||
const MockConnection = buildMockConnection({
|
||||
onRequest (params) {
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: '<html><body>error!</body></html>',
|
||||
headers: { 'content-type': 'text/html' }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const client = new Client({
|
||||
node: 'http://localhost:9200',
|
||||
Connection: MockConnection,
|
||||
maxRetries: 1
|
||||
})
|
||||
|
||||
client.info((err, result) => {
|
||||
t.ok(err instanceof errors.ResponseError)
|
||||
t.equal(err.name, 'ResponseError')
|
||||
t.equal(err.body, '<html><body>error!</body></html>')
|
||||
t.equal(err.message, '<html><body>error!</body></html>')
|
||||
t.equal(err.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
||||
@ -197,3 +197,29 @@ test('ResponseError with meaningful message / 3', t => {
|
||||
t.equal(err.toString(), JSON.stringify(meta.body))
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('ResponseError with meaningful message when body is not json', t => {
|
||||
const meta = {
|
||||
statusCode: 400,
|
||||
body: '<html><body>error!</body></html>',
|
||||
headers: { 'content-type': 'text/html' }
|
||||
}
|
||||
const err = new errors.ResponseError(meta)
|
||||
t.equal(err.name, 'ResponseError')
|
||||
t.equal(err.message, '<html><body>error!</body></html>')
|
||||
t.equal(err.toString(), JSON.stringify(meta.body))
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('ResponseError with meaningful message when body is falsy', t => {
|
||||
const meta = {
|
||||
statusCode: 400,
|
||||
body: '',
|
||||
headers: { 'content-type': 'text/plain' }
|
||||
}
|
||||
const err = new errors.ResponseError(meta)
|
||||
t.equal(err.name, 'ResponseError')
|
||||
t.equal(err.message, 'Response Error')
|
||||
t.equal(err.toString(), JSON.stringify(meta.body))
|
||||
t.end()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user