Skip to content

Commit

Permalink
Improve kql error message handling and avoid fetcihng twice (#54239) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Jan 10, 2020
1 parent 921e933 commit d95d799
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
});

Expand Down Expand Up @@ -817,6 +819,7 @@ function discoverController(
title: i18n.translate('kbn.discover.errorLoadingData', {
defaultMessage: 'Error loading data',
}),
toastMessage: error.shortMessage,
});
}
});
Expand Down
27 changes: 15 additions & 12 deletions src/plugins/data/common/es_query/kuery/kuery_syntax_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const grammarRuleTranslations: Record<string, string> = {

interface KQLSyntaxErrorData extends Error {
found: string;
expected: KQLSyntaxErrorExpected[];
expected: KQLSyntaxErrorExpected[] | null;
location: any;
}

Expand All @@ -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'
Expand Down

0 comments on commit d95d799

Please sign in to comment.