Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions src/plugins/data/common/search/aggs/agg_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
21 changes: 21 additions & 0 deletions src/plugins/data/common/search/aggs/agg_configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
19 changes: 17 additions & 2 deletions src/plugins/data/common/search/aggs/agg_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
});
}

Expand Down