-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ESUI] More robust handling of error responses (#96819)
* more robust handling of error responses * added tests and further hardening of how we handle error values
- Loading branch information
1 parent
d8b4316
commit b9c4d24
Showing
2 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/plugins/es_ui_shared/__packages_do_not_import__/errors/handle_es_error.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { errors } from '@elastic/elasticsearch'; | ||
// eslint-disable-next-line @kbn/eslint/no-restricted-paths | ||
import { kibanaResponseFactory as response } from 'src/core/server'; | ||
import { handleEsError } from './handle_es_error'; | ||
|
||
const { ResponseError } = errors; | ||
|
||
const anyObject: any = {}; | ||
|
||
describe('handleEsError', () => { | ||
test('top-level reason is an empty string', () => { | ||
const emptyReasonError = new ResponseError({ | ||
warnings: [], | ||
meta: anyObject, | ||
body: { | ||
error: { | ||
root_cause: [], | ||
type: 'search_phase_execution_exception', | ||
reason: '', // Empty reason | ||
phase: 'fetch', | ||
grouped: true, | ||
failed_shards: [], | ||
caused_by: { | ||
type: 'too_many_buckets_exception', | ||
reason: 'This is the nested reason', | ||
max_buckets: 100, | ||
}, | ||
}, | ||
}, | ||
statusCode: 503, | ||
headers: {}, | ||
}); | ||
|
||
const { payload, status } = handleEsError({ error: emptyReasonError, response }); | ||
|
||
expect(payload.message).toEqual('This is the nested reason'); | ||
expect(status).toBe(503); | ||
}); | ||
|
||
test('empty error', () => { | ||
const { payload, status } = handleEsError({ | ||
error: new ResponseError({ | ||
body: {}, | ||
statusCode: 400, | ||
headers: {}, | ||
meta: anyObject, | ||
warnings: [], | ||
}), | ||
response, | ||
}); | ||
|
||
expect(payload).toEqual({ | ||
attributes: { causes: undefined, error: undefined }, | ||
message: 'Response Error', | ||
}); | ||
|
||
expect(status).toBe(400); | ||
}); | ||
|
||
test('unknown object', () => { | ||
expect(() => handleEsError({ error: anyObject, response })).toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters