Skip to content

Commit

Permalink
[ES|QL] Fix error handling for ES|QL nested error messages (#170005)
Browse files Browse the repository at this point in the history
## Summary

Fix #170004

This PR fixes the wrong behaviour with message extraction from this type
of ES|QL responses.
Took also the opportunity to improve the FTR side, with some monaco
handlers for errors in the editor and some test cases for errors in
ES|QL queries.


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
dej611 and kibanamachine authored Nov 1, 2023
1 parent bc90e56 commit 2643f50
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/plugins/data/common/search/expressions/esql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ function sanitize(value: string) {
return value.replace(/[\(\)]/g, '_');
}

function extractTypeAndReason(attributes: any): { type?: string; reason?: string } {
if (['type', 'reason'].every((prop) => prop in attributes)) {
return attributes;
}
if ('error' in attributes) {
return extractTypeAndReason(attributes.error);
}
return {};
}

interface ESQLSearchParams {
time_zone?: string;
query: string;
Expand Down Expand Up @@ -199,7 +209,7 @@ export const getEsqlFn = ({ getStartDependencies }: EsqlFnArguments) => {
if (!error.err) {
error.message = `Unexpected error from Elasticsearch: ${error.message}`;
} else {
const { type, reason } = error.err.attributes;
const { type, reason } = extractTypeAndReason(error.err.attributes);
if (type === 'parsing_exception') {
error.message = `Couldn't parse Elasticsearch ES|QL query. Check your query and try again. Error: ${reason}`;
} else {
Expand Down
27 changes: 27 additions & 0 deletions test/functional/apps/discover/group4/_esql_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,32 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
expect(await cell.getVisibleText()).to.be('1');
});
});
describe('errors', () => {
it('should error messages for syntax errors in query', async function () {
await PageObjects.discover.selectTextBaseLang();
const brokenQueries = [
'from logstash-* | limit 10*',
'from logstash-* | limit A',
'from logstash-* | where a*',
'limit 10',
];
for (const testQuery of brokenQueries) {
await monacoEditor.setCodeEditorValue(testQuery);
await testSubjects.click('querySubmitButton');
await PageObjects.header.waitUntilLoadingHasFinished();
await PageObjects.discover.waitUntilSearchingHasFinished();
// error in fetching documents because of the invalid query
await testSubjects.existOrFail('discoverNoResultsError');
const message = await testSubjects.getVisibleText('discoverErrorCalloutMessage');
expect(message).to.contain(
"[esql] > Couldn't parse Elasticsearch ES|QL query. Check your query and try again."
);
expect(message).to.not.contain('undefined');
if (message.includes('line')) {
expect((await monacoEditor.getCurrentMarkers('kibanaCodeEditor')).length).to.eql(1);
}
}
});
});
});
}
7 changes: 7 additions & 0 deletions test/functional/services/monaco_editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class MonacoEditorService extends FtrService {
private readonly retry = this.ctx.getService('retry');
private readonly browser = this.ctx.getService('browser');
private readonly testSubjects = this.ctx.getService('testSubjects');
private readonly findService = this.ctx.getService('find');

public async waitCodeEditorReady(containerTestSubjId: string) {
const editorContainer = await this.testSubjects.find(containerTestSubjId);
Expand Down Expand Up @@ -52,4 +53,10 @@ export class MonacoEditorService extends FtrService {
);
});
}

public async getCurrentMarkers(testSubjId: string) {
return this.findService.allByCssSelector(
`[data-test-subj="${testSubjId}"] .cdr.squiggly-error`
);
}
}

0 comments on commit 2643f50

Please sign in to comment.