Skip to content

Commit 6df0f6a

Browse files
committed
[data.search.aggs] Throw an error when trying to create an agg type that doesn't exist. (#81509)
1 parent 04cfe46 commit 6df0f6a

3 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/plugins/data/common/search/aggs/agg_config.test.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -680,16 +680,6 @@ describe('AggConfig', () => {
680680
const json = aggConfig.toExpressionAst()?.arguments.json;
681681
expect(json).toEqual([JSON.stringify(configStates.params.json)]);
682682
});
683-
684-
it(`returns undefined if an expressionName doesn't exist on the agg type`, () => {
685-
const ac = new AggConfigs(indexPattern, [], { typesRegistry });
686-
const configStates = {
687-
type: 'unknown type',
688-
params: {},
689-
};
690-
const aggConfig = ac.createAggConfig(configStates);
691-
expect(aggConfig.toExpressionAst()).toBe(undefined);
692-
});
693683
});
694684

695685
describe('#makeLabel', () => {

src/plugins/data/common/search/aggs/agg_configs.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,27 @@ describe('AggConfigs', () => {
150150
);
151151
expect(ac.aggs).toHaveLength(1);
152152
});
153+
154+
it(`throws if trying to add an agg which doesn't have a type in the registry`, () => {
155+
const configStates = [
156+
{
157+
enabled: true,
158+
type: 'histogram',
159+
params: {},
160+
},
161+
];
162+
163+
const ac = new AggConfigs(indexPattern, configStates, { typesRegistry });
164+
expect(() =>
165+
ac.createAggConfig({
166+
enabled: true,
167+
type: 'oops',
168+
params: {},
169+
})
170+
).toThrowErrorMatchingInlineSnapshot(
171+
`"Unable to find a registered agg type for \\"oops\\"."`
172+
);
173+
});
153174
});
154175

155176
describe('#getRequestAggs', () => {

src/plugins/data/common/search/aggs/agg_configs.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import _ from 'lodash';
21+
import { i18n } from '@kbn/i18n';
2122
import { Assign } from '@kbn/utility-types';
2223

2324
import { ISearchOptions, ISearchSource } from 'src/plugins/data/public';
@@ -122,15 +123,29 @@ export class AggConfigs {
122123
{ addToAggConfigs = true } = {}
123124
) => {
124125
const { type } = params;
125-
let aggConfig;
126+
const getType = (t: string) => {
127+
const typeFromRegistry = this.typesRegistry.get(t);
128+
129+
if (!typeFromRegistry) {
130+
throw new Error(
131+
i18n.translate('data.search.aggs.error.aggNotFound', {
132+
defaultMessage: 'Unable to find a registered agg type for "{type}".',
133+
values: { type: type as string },
134+
})
135+
);
136+
}
126137

138+
return typeFromRegistry;
139+
};
140+
141+
let aggConfig;
127142
if (params instanceof AggConfig) {
128143
aggConfig = params;
129144
params.parent = this;
130145
} else {
131146
aggConfig = new AggConfig(this, {
132147
...params,
133-
type: typeof type === 'string' ? this.typesRegistry.get(type) : type,
148+
type: typeof type === 'string' ? getType(type) : type,
134149
});
135150
}
136151

0 commit comments

Comments
 (0)