From bf02b3d0e51612369abbc407b6c30e7c4b61e01b Mon Sep 17 00:00:00 2001 From: Tomas Della Vedova Date: Wed, 19 May 2021 11:09:19 +0200 Subject: [PATCH] Add top level type error to error message (#1468) --- lib/errors.js | 5 +++-- test/types/kibana.test-d.ts | 12 ++++++------ test/types/new-types.test-d.ts | 12 ++++++------ test/unit/errors.test.js | 26 ++++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/errors.js b/lib/errors.js index 657419c8e..cf2f296d8 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -92,9 +92,10 @@ class ResponseError extends ElasticsearchClientError { this.name = 'ResponseError' 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('; ') + this.message = meta.body.error.type + ': ' + this.message += meta.body.error.root_cause.map(entry => `[${entry.type}] Reason: ${entry.reason}`).join('; ') } else { - this.message = 'Response Error' + this.message = meta.body.error.type } } else { this.message = 'Response Error' diff --git a/test/types/kibana.test-d.ts b/test/types/kibana.test-d.ts index 139d21035..6385ebd02 100644 --- a/test/types/kibana.test-d.ts +++ b/test/types/kibana.test-d.ts @@ -66,27 +66,27 @@ client.on('resurrect', (err, meta) => { // Check API returned type and optional parameters { const promise = client.info() - expectType>>(promise) + expectType>>(promise) promise - .then(result => expectType>(result)) + .then(result => expectType>(result)) .catch((err: ApiError) => expectType(err)) expectType(promise.abort()) } { const promise = client.info({ pretty: true }) - expectType>>(promise) + expectType>>(promise) promise - .then(result => expectType>(result)) + .then(result => expectType>(result)) .catch((err: ApiError) => expectType(err)) expectType(promise.abort()) } { const promise = client.info({ pretty: true }, { ignore: [404] }) - expectType>>(promise) + expectType>>(promise) promise - .then(result => expectType>(result)) + .then(result => expectType>(result)) .catch((err: ApiError) => expectType(err)) expectType(promise.abort()) } diff --git a/test/types/new-types.test-d.ts b/test/types/new-types.test-d.ts index 9d35666a5..4a237dcf4 100644 --- a/test/types/new-types.test-d.ts +++ b/test/types/new-types.test-d.ts @@ -66,27 +66,27 @@ client.on('resurrect', (err, meta) => { // Check API returned type and optional parameters { const promise = client.info() - expectType>>(promise) + expectType>>(promise) promise - .then(result => expectType>(result)) + .then(result => expectType>(result)) .catch((err: ApiError) => expectType(err)) expectType(promise.abort()) } { const promise = client.info({ pretty: true }) - expectType>>(promise) + expectType>>(promise) promise - .then(result => expectType>(result)) + .then(result => expectType>(result)) .catch((err: ApiError) => expectType(err)) expectType(promise.abort()) } { const promise = client.info({ pretty: true }, { ignore: [404] }) - expectType>>(promise) + expectType>>(promise) promise - .then(result => expectType>(result)) + .then(result => expectType>(result)) .catch((err: ApiError) => expectType(err)) expectType(promise.abort()) } diff --git a/test/unit/errors.test.js b/test/unit/errors.test.js index 7c3f706a1..783f1b2f6 100644 --- a/test/unit/errors.test.js +++ b/test/unit/errors.test.js @@ -131,7 +131,7 @@ test('ResponseError with meaningful message / 1', t => { headers: {} } const err = new errors.ResponseError(meta) - t.strictEqual(err.message, '[index_not_found_exception] Reason: no such index [foo]') + t.strictEqual(err.message, 'index_not_found_exception: [index_not_found_exception] Reason: no such index [foo]') t.strictEqual(err.toString(), JSON.stringify(meta.body)) t.end() }) @@ -171,7 +171,29 @@ test('ResponseError with meaningful message / 2', t => { 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.message, 'index_not_found_exception: [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() +}) + +test('ResponseError with meaningful message / 3', t => { + const meta = { + body: { + error: { + 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') t.strictEqual(err.toString(), JSON.stringify(meta.body)) t.end() })