Improve kql error message handling and avoid fetcihng twice (#54239)

This commit is contained in:
Joe Reuter 2020-01-10 16:53:24 +01:00 committed by GitHub
parent d8f94b1792
commit 6cbfa8ee3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 13 deletions

View file

@ -671,7 +671,9 @@ function discoverController(
$scope.$watch('state.query', (newQuery, oldQuery) => {
if (!_.isEqual(newQuery, oldQuery)) {
const query = migrateLegacyQuery(newQuery);
$scope.updateQueryAndFetch({ query });
if (!_.isEqual(query, newQuery)) {
$scope.updateQueryAndFetch({ query });
}
}
});
@ -817,6 +819,7 @@ function discoverController(
title: i18n.translate('kbn.discover.errorLoadingData', {
defaultMessage: 'Error loading data',
}),
toastMessage: error.shortMessage,
});
}
});

View file

@ -41,7 +41,7 @@ const grammarRuleTranslations: Record<string, string> = {
interface KQLSyntaxErrorData extends Error {
found: string;
expected: KQLSyntaxErrorExpected[];
expected: KQLSyntaxErrorExpected[] | null;
location: any;
}
@ -53,19 +53,22 @@ export class KQLSyntaxError extends Error {
shortMessage: string;
constructor(error: KQLSyntaxErrorData, expression: any) {
const translatedExpectations = error.expected.map(expected => {
return grammarRuleTranslations[expected.description] || expected.description;
});
let message = error.message;
if (error.expected) {
const translatedExpectations = error.expected.map(expected => {
return grammarRuleTranslations[expected.description] || expected.description;
});
const translatedExpectationText = translatedExpectations.join(', ');
const translatedExpectationText = translatedExpectations.join(', ');
const message = i18n.translate('data.common.esQuery.kql.errors.syntaxError', {
defaultMessage: 'Expected {expectedList} but {foundInput} found.',
values: {
expectedList: translatedExpectationText,
foundInput: error.found ? `"${error.found}"` : endOfInputText,
},
});
message = i18n.translate('data.common.esQuery.kql.errors.syntaxError', {
defaultMessage: 'Expected {expectedList} but {foundInput} found.',
values: {
expectedList: translatedExpectationText,
foundInput: error.found ? `"${error.found}"` : endOfInputText,
},
});
}
const fullMessage = [message, expression, repeat('-', error.location.start.offset) + '^'].join(
'\n'