[data.search.aggs] Throw an error when trying to create an agg type that doesn't exist. (#81509)

This commit is contained in:
Luke Elmers 2020-11-09 17:33:13 -07:00 committed by GitHub
parent 7a67cffe37
commit f193d9c962
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 12 deletions

View file

@ -680,16 +680,6 @@ describe('AggConfig', () => {
const json = aggConfig.toExpressionAst()?.arguments.json;
expect(json).toEqual([JSON.stringify(configStates.params.json)]);
});
it(`returns undefined if an expressionName doesn't exist on the agg type`, () => {
const ac = new AggConfigs(indexPattern, [], { typesRegistry });
const configStates = {
type: 'unknown type',
params: {},
};
const aggConfig = ac.createAggConfig(configStates);
expect(aggConfig.toExpressionAst()).toBe(undefined);
});
});
describe('#makeLabel', () => {

View file

@ -150,6 +150,27 @@ describe('AggConfigs', () => {
);
expect(ac.aggs).toHaveLength(1);
});
it(`throws if trying to add an agg which doesn't have a type in the registry`, () => {
const configStates = [
{
enabled: true,
type: 'histogram',
params: {},
},
];
const ac = new AggConfigs(indexPattern, configStates, { typesRegistry });
expect(() =>
ac.createAggConfig({
enabled: true,
type: 'oops',
params: {},
})
).toThrowErrorMatchingInlineSnapshot(
`"Unable to find a registered agg type for \\"oops\\"."`
);
});
});
describe('#getRequestAggs', () => {

View file

@ -18,6 +18,7 @@
*/
import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { Assign } from '@kbn/utility-types';
import { ISearchOptions, ISearchSource } from 'src/plugins/data/public';
@ -122,15 +123,29 @@ export class AggConfigs {
{ addToAggConfigs = true } = {}
) => {
const { type } = params;
let aggConfig;
const getType = (t: string) => {
const typeFromRegistry = this.typesRegistry.get(t);
if (!typeFromRegistry) {
throw new Error(
i18n.translate('data.search.aggs.error.aggNotFound', {
defaultMessage: 'Unable to find a registered agg type for "{type}".',
values: { type: type as string },
})
);
}
return typeFromRegistry;
};
let aggConfig;
if (params instanceof AggConfig) {
aggConfig = params;
params.parent = this;
} else {
aggConfig = new AggConfig(this, {
...params,
type: typeof type === 'string' ? this.typesRegistry.get(type) : type,
type: typeof type === 'string' ? getType(type) : type,
});
}