diff --git a/packages/kbn-generate-csv/src/generate_csv.ts b/packages/kbn-generate-csv/src/generate_csv.ts index cafc6600f51a3..5ed92df84c581 100644 --- a/packages/kbn-generate-csv/src/generate_csv.ts +++ b/packages/kbn-generate-csv/src/generate_csv.ts @@ -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'; @@ -235,6 +237,21 @@ export class CsvGenerator { public async generateData(): Promise { 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, @@ -243,7 +260,7 @@ export class CsvGenerator { this.job.browserTimezone, logger ), - this.dependencies.searchSourceStart.create(this.job.searchSource), + createSearchSource(), ]); const { startedAt, retryAt } = this.taskInstanceFields; diff --git a/packages/kbn-generate-csv/tsconfig.json b/packages/kbn-generate-csv/tsconfig.json index b57990c20eb4a..4216438b6689a 100644 --- a/packages/kbn-generate-csv/tsconfig.json +++ b/packages/kbn-generate-csv/tsconfig.json @@ -30,5 +30,6 @@ "@kbn/es-types", "@kbn/data-views-plugin", "@kbn/search-types", + "@kbn/task-manager-plugin", ] } diff --git a/packages/kbn-reporting/common/errors.ts b/packages/kbn-reporting/common/errors.ts index 9f45a1b6ae1d5..45a299115bf1b 100644 --- a/packages/kbn-reporting/common/errors.ts +++ b/packages/kbn-reporting/common/errors.ts @@ -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; + } +}