File tree Expand file tree Collapse file tree
src/plugins/data/common/search/aggs Expand file tree Collapse file tree Original file line number Diff line number Diff 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' , ( ) => {
Original file line number Diff line number Diff 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' , ( ) => {
Original file line number Diff line number Diff line change 1818 */
1919
2020import _ from 'lodash' ;
21+ import { i18n } from '@kbn/i18n' ;
2122import { Assign } from '@kbn/utility-types' ;
2223
2324import { 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
You can’t perform that action at this time.
0 commit comments