[input controls] safely handle case where control index pattern no longer exists (#18931) (#19051)

* handle case where control index pattern no longer exists

* fix errors when vis is edited with missing index pattern
This commit is contained in:
Nathan Reese 2018-05-14 15:09:09 -06:00 committed by GitHub
parent e92060cf1b
commit 595d24dc3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 6 deletions

View file

@ -36,7 +36,13 @@ export class FieldSelect extends Component {
return;
}
const indexPattern = await this.props.getIndexPattern(indexPatternId);
let indexPattern;
try {
indexPattern = await this.props.getIndexPattern(indexPatternId);
} catch (err) {
// index pattern no longer exists
return;
}
// props.indexPatternId may be updated before getIndexPattern returns
// ignore response when fetched index pattern does not match active index pattern

View file

@ -6,9 +6,14 @@ which doesn't exist on any documents in the "${indexPatternName}" index pattern.
Choose a different field or index documents that contain values for this field.`;
}
export function noIndexPatternMsg(indexPatternId) {
return `Could not locate index-pattern id: ${indexPatternId}.`;
}
export class Control {
constructor(controlParams, filterManager, kbnApi, useTimeFilter) {
this.id = controlParams.id;
this.controlParams = controlParams;
this.options = controlParams.options;
this.type = controlParams.type;
this.label = controlParams.label ? controlParams.label : controlParams.fieldName;

View file

@ -1,7 +1,8 @@
import _ from 'lodash';
import {
Control,
noValuesDisableMsg
noValuesDisableMsg,
noIndexPatternMsg,
} from './control';
import { PhraseFilterManager } from './filter_manager/phrase_filter_manager';
import { createSearchSource } from './create_search_source';
@ -41,6 +42,12 @@ class ListControl extends Control {
}
async fetch() {
const indexPattern = this.filterManager.getIndexPattern();
if (!indexPattern) {
this.disable(noIndexPatternMsg(this.controlParams.indexPattern));
return;
}
let ancestorFilters;
if (this.hasAncestors()) {
if (this.hasUnsetAncestor()) {
@ -58,7 +65,6 @@ class ListControl extends Control {
ancestorFilters = this.getAncestorFilters();
}
const indexPattern = this.filterManager.getIndexPattern();
const fieldName = this.filterManager.fieldName;
const initialSearchSourceState = {
timeout: '1s',
@ -95,7 +101,12 @@ class ListControl extends Control {
}
export async function listControlFactory(controlParams, kbnApi, useTimeFilter) {
const indexPattern = await kbnApi.indexPatterns.get(controlParams.indexPattern);
let indexPattern;
try {
indexPattern = await kbnApi.indexPatterns.get(controlParams.indexPattern);
} catch (err) {
// ignore not found error and return control so it can be displayed in disabled state.
}
return new ListControl(
controlParams,

View file

@ -1,7 +1,8 @@
import _ from 'lodash';
import {
Control,
noValuesDisableMsg
noValuesDisableMsg,
noIndexPatternMsg,
} from './control';
import { RangeFilterManager } from './filter_manager/range_filter_manager';
import { createSearchSource } from './create_search_source';
@ -30,6 +31,11 @@ class RangeControl extends Control {
async fetch() {
const indexPattern = this.filterManager.getIndexPattern();
if (!indexPattern) {
this.disable(noIndexPatternMsg(this.controlParams.indexPattern));
return;
}
const fieldName = this.filterManager.fieldName;
const aggs = minMaxAgg(indexPattern.fields.byName[fieldName]);
@ -60,7 +66,12 @@ class RangeControl extends Control {
}
export async function rangeControlFactory(controlParams, kbnApi, useTimeFilter) {
const indexPattern = await kbnApi.indexPatterns.get(controlParams.indexPattern);
let indexPattern;
try {
indexPattern = await kbnApi.indexPatterns.get(controlParams.indexPattern);
} catch (err) {
// ignore not found error and return control so it can be displayed in disabled state.
}
const unsetValue = { min: 0, max: 1 };
return new RangeControl(
controlParams,