[Reporting/CSV] Insert error message into contents if export is empty (#175852)

## Summary

Closes https://github.com/elastic/kibana/issues/174659

### Release note
Updated CSV export to insert error messages into the contents if the
export results in an empty file due to an error.

### Checklist

Delete any items that are not applicable to this PR.

- [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
This commit is contained in:
Tim Sullivan 2024-01-31 08:47:29 -07:00 committed by GitHub
parent c0fd8e01ae
commit 9f84b98933
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 0 deletions

View file

@ -1280,6 +1280,7 @@ describe('CsvGenerator', () => {
});
it('handles unknown errors', async () => {
const streamWriteSpy = jest.spyOn(stream, 'write');
mockDataClient.search = jest.fn().mockImplementation(() => {
throw new Error('An unknown error');
});
@ -1300,6 +1301,7 @@ describe('CsvGenerator', () => {
mockLogger,
stream
);
await expect(generateCsv.generateData()).resolves.toMatchInlineSnapshot(`
Object {
"content_type": "text/csv",
@ -1317,6 +1319,18 @@ describe('CsvGenerator', () => {
],
}
`);
expect(streamWriteSpy.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"",
],
Array [
"
\\"Encountered an unknown error: An unknown error\\"
\\"Encountered an error with the number of CSV rows generated from the search: expected rows were indeterminable, received 0.\\"",
],
]
`);
});
describe('error codes', () => {

View file

@ -401,6 +401,24 @@ export class CsvGenerator {
}
}
if (this.csvRowCount === 0) {
if (warnings.length > 0) {
/*
* Add the errors into the CSV content. This makes error messages more
* discoverable. When the export was automated or triggered by an API
* call or is automated, the user doesn't necesssarily go through the
* Kibana UI to download the export and might not otherwise see the
* error message.
*/
logger.info('CSV export content was empty. Adding error messages to CSV export content.');
// join the string array and putting double quotes around each item
// add a leading newline so the first message is not treated as a header
builder.tryAppend('\n"' + warnings.join('"\n"') + '"');
} else {
logger.info('CSV export content was empty. No error messages.');
}
}
return {
content_type: CONTENT_TYPE_CSV,
csv_contains_formulas: this.csvContainsFormulas && !escapeFormulaValues,