Skip to content

Commit

Permalink
[Reporting] Add searchSourceStart.create error handling (#197238)
Browse files Browse the repository at this point in the history
## Summary

This PR adds a new type of error (`ReportingSavedObjectNotFoundError`)
which gets thrown when passed in saved object doesn't eixst.

This produces a log like this:
```
[2024-10-22T15:09:26.768+02:00][ERROR][plugins.reporting.runTask] Error: ReportingError(code: reporting_saved_object_not_found) "Error: Saved object [index-pattern/ff959d40-b880-11e8-a6d9-e546fe2bba5f] not found"
```

Closes: #191548
Closes: #196620

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
kowalczyk-krzysztof and kibanamachine authored Oct 22, 2024
1 parent c417196 commit ae0ac74
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/kbn-generate-csv/src/generate_csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import {
byteSizeValueToNumber,
CancellationToken,
ReportingError,
ReportingSavedObjectNotFoundError,
} from '@kbn/reporting-common';
import type { TaskInstanceFields, TaskRunResult } from '@kbn/reporting-common/types';
import type { ReportingConfigType } from '@kbn/reporting-server';

import { TaskErrorSource, createTaskRunError } from '@kbn/task-manager-plugin/server';
import { CONTENT_TYPE_CSV } from '../constants';
import type { JobParamsCSV } from '../types';
import { getExportSettings, type CsvExportSettings } from './lib/get_export_settings';
Expand Down Expand Up @@ -235,6 +237,21 @@ export class CsvGenerator {

public async generateData(): Promise<TaskRunResult> {
const logger = this.logger;

const createSearchSource = async () => {
try {
const source = await this.dependencies.searchSourceStart.create(this.job.searchSource);
return source;
} catch (err) {
// Saved object not found
if (err?.output?.statusCode === 404) {
const reportingError = new ReportingSavedObjectNotFoundError(err);
throw createTaskRunError(reportingError, TaskErrorSource.USER);
}
throw err;
}
};

const [settings, searchSource] = await Promise.all([
getExportSettings(
this.clients.uiSettings,
Expand All @@ -243,7 +260,7 @@ export class CsvGenerator {
this.job.browserTimezone,
logger
),
this.dependencies.searchSourceStart.create(this.job.searchSource),
createSearchSource(),
]);

const { startedAt, retryAt } = this.taskInstanceFields;
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-generate-csv/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"@kbn/es-types",
"@kbn/data-views-plugin",
"@kbn/search-types",
"@kbn/task-manager-plugin",
]
}
7 changes: 7 additions & 0 deletions packages/kbn-reporting/common/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,10 @@ export class VisualReportingSoftDisabledError extends ReportingError {
});
}
}

export class ReportingSavedObjectNotFoundError extends ReportingError {
static code = 'reporting_saved_object_not_found_error' as const;
public get code(): string {
return ReportingSavedObjectNotFoundError.code;
}
}

0 comments on commit ae0ac74

Please sign in to comment.