From 559acd32be8c4d00e416caae9e19db930a9a1b2f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 11 Aug 2021 09:46:37 +0200 Subject: [PATCH] [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 --- lib/errors.js | 4 +++- test/acceptance/product-check.test.js | 2 +- test/unit/client.test.js | 31 ++++++++++++++++++++++++++- test/unit/errors.test.js | 26 ++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/lib/errors.js b/lib/errors.js index 2ec9bc715..bc691c86b 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -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 } diff --git a/test/acceptance/product-check.test.js b/test/acceptance/product-check.test.js index e32908aef..30c50eaa2 100644 --- a/test/acceptance/product-check.test.js +++ b/test/acceptance/product-check.test.js @@ -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', diff --git a/test/unit/client.test.js b/test/unit/client.test.js index fbc45dc82..cd6484ffe 100644 --- a/test/unit/client.test.js +++ b/test/unit/client.test.js @@ -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: 'error!', + 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, 'error!') + t.equal(err.message, 'error!') + t.equal(err.statusCode, 400) + }) +}) diff --git a/test/unit/errors.test.js b/test/unit/errors.test.js index 301ca1108..b8db815d6 100644 --- a/test/unit/errors.test.js +++ b/test/unit/errors.test.js @@ -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: 'error!', + headers: { 'content-type': 'text/html' } + } + const err = new errors.ResponseError(meta) + t.equal(err.name, 'ResponseError') + t.equal(err.message, 'error!') + 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() +})