[5.5] [api/indexPatterns/fields] convert all es errors to Boom errors (#12435)

This commit is contained in:
Spencer 2017-06-20 21:35:33 -04:00 committed by GitHub
parent 2956aa04ef
commit 6d605ec30a
4 changed files with 41 additions and 29 deletions

View file

@ -1,8 +1,8 @@
import sinon from 'sinon';
import expect from 'expect.js';
import { convertEsIndexNotFoundError } from '../errors';
import * as convertEsIndexNotFoundErrorNS from '../errors';
import { convertEsError } from '../errors';
import * as convertEsErrorNS from '../errors';
import { callIndexAliasApi, callFieldCapsApi } from '../es_api';
@ -45,21 +45,21 @@ describe('server/index_patterns/service/lib/es_api', () => {
expect(passedOpts).to.have.property('allowNoIndices', false);
});
it('handles errors with convertEsIndexNotFoundError()', async () => {
it('handles errors with convertEsError()', async () => {
const indices = [];
const esError = new Error('esError');
const convertedError = new Error('convertedError');
sandbox.stub(convertEsIndexNotFoundErrorNS, 'convertEsIndexNotFoundError', () => { throw convertedError; });
sandbox.stub(convertEsErrorNS, 'convertEsError', () => { throw convertedError; });
const callCluster = sinon.spy(async () => { throw esError; });
try {
await callIndexAliasApi(callCluster, indices);
throw new Error('expected callIndexAliasApi() to throw');
} catch (error) {
expect(error).to.be(convertedError);
sinon.assert.calledOnce(convertEsIndexNotFoundError);
expect(convertEsIndexNotFoundError.args[0][0]).to.be(indices);
expect(convertEsIndexNotFoundError.args[0][1]).to.be(esError);
sinon.assert.calledOnce(convertEsError);
expect(convertEsError.args[0][0]).to.be(indices);
expect(convertEsError.args[0][1]).to.be(esError);
}
});
});
@ -103,21 +103,21 @@ describe('server/index_patterns/service/lib/es_api', () => {
expect(passedOpts).to.have.property('allowNoIndices', false);
});
it('handles errors with convertEsIndexNotFoundError()', async () => {
it('handles errors with convertEsError()', async () => {
const indices = [];
const esError = new Error('esError');
const convertedError = new Error('convertedError');
sandbox.stub(convertEsIndexNotFoundErrorNS, 'convertEsIndexNotFoundError', () => { throw convertedError; });
sandbox.stub(convertEsErrorNS, 'convertEsError', () => { throw convertedError; });
const callCluster = sinon.spy(async () => { throw esError; });
try {
await callFieldCapsApi(callCluster, indices);
throw new Error('expected callFieldCapsApi() to throw');
} catch (error) {
expect(error).to.be(convertedError);
sinon.assert.calledOnce(convertEsIndexNotFoundError);
expect(convertEsIndexNotFoundError.args[0][0]).to.be(indices);
expect(convertEsIndexNotFoundError.args[0][1]).to.be(esError);
sinon.assert.calledOnce(convertEsError);
expect(convertEsError.args[0][0]).to.be(indices);
expect(convertEsError.args[0][1]).to.be(esError);
}
});
});

View file

@ -42,10 +42,12 @@ export function isNoMatchingIndicesError(err) {
* @param {[type]} indices [description]
* @return {[type]} [description]
*/
export function convertEsIndexNotFoundError(indices, error) {
export function convertEsError(indices, error) {
if (isEsIndexNotFoundError(error)) {
return createNoMatchingIndicesError(indices);
}
return error;
const statusCode = error.statusCode;
const message = error.body ? error.body.error : undefined;
return Boom.wrap(error, statusCode, message);
}

View file

@ -1,4 +1,4 @@
import { convertEsIndexNotFoundError } from './errors';
import { convertEsError } from './errors';
/**
* Call the index.getAlias API for a list of indices.
@ -22,7 +22,7 @@ export async function callIndexAliasApi(callCluster, indices) {
allowNoIndices: false
});
} catch (error) {
throw convertEsIndexNotFoundError(indices, error);
throw convertEsError(indices, error);
}
}
@ -46,6 +46,6 @@ export async function callFieldCapsApi(callCluster, indices) {
allowNoIndices: false
});
} catch (error) {
throw convertEsIndexNotFoundError(indices, error);
throw convertEsError(indices, error);
}
}

View file

@ -1,10 +1,11 @@
import expect from 'expect.js';
import { errors as esErrors } from 'elasticsearch';
import {
isEsIndexNotFoundError,
createNoMatchingIndicesError,
isNoMatchingIndicesError,
convertEsIndexNotFoundError
convertEsError
} from '../../../../../src/server/index_patterns/service/lib/errors';
import {
@ -76,25 +77,34 @@ export default function ({ getService }) {
});
});
describe('convertEsIndexNotFoundError()', () => {
describe('convertEsError()', () => {
const indices = ['foo', 'bar'];
it('converts indexNotFoundErrors into NoMatchingIndices errors', async () => {
const converted = convertEsIndexNotFoundError(indices, indexNotFoundError);
const converted = convertEsError(indices, indexNotFoundError);
if (!isNoMatchingIndicesError(converted)) {
throw new Error('expected convertEsIndexNotFoundError(indexNotFoundError) to return NoMatchingIndices error');
throw new Error('expected convertEsError(indexNotFoundError) to return NoMatchingIndices error');
}
});
it('returns other errors', async () => {
const originals = [docNotFoundError, '', 1, /foo/, new Date(), new Error(), function () {}];
originals.forEach(orig => {
const converted = convertEsIndexNotFoundError(indices, orig);
if (converted !== orig) {
throw new Error(`expected convertEsIndexNotFoundError(${orig}) to return original error`);
}
it('wraps other errors in Boom', async () => {
const error = new esErrors.AuthenticationException({
root_cause: [
{
type: 'security_exception',
reason: 'action [indices:data/read/field_caps] is unauthorized for user [standard]'
}
],
type: 'security_exception',
reason: 'action [indices:data/read/field_caps] is unauthorized for user [standard]'
}, {
statusCode: 403
});
expect(error).to.not.have.property('isBoom');
const converted = convertEsError(indices, error);
expect(converted).to.have.property('isBoom');
expect(converted.output.statusCode).to.be(403);
});
});
});