diff --git a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts index 8f321a266c..1eb32c882d 100644 --- a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts +++ b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts @@ -362,13 +362,16 @@ export class ElasticSearchSearchEngine implements SearchEngine { nextPageCursor, previousPageCursor, }; - } catch (e) { - if (e.meta.body.error.type === 'index_not_found_exception') { - throw new MissingIndexError(`Missing index for ${queryIndices}`, e); + } catch (error) { + if (error.meta.body.error.type === 'index_not_found_exception') { + throw new MissingIndexError( + `Missing index for ${queryIndices}. This means there are no documents to search through.`, + error, + ); } this.logger.error( `Failed to query documents for indices ${queryIndices}`, - e, + error, ); return Promise.reject({ results: [] }); } diff --git a/plugins/search-backend-node/api-report.md b/plugins/search-backend-node/api-report.md index 63273d7514..ff9480c19d 100644 --- a/plugins/search-backend-node/api-report.md +++ b/plugins/search-backend-node/api-report.md @@ -6,7 +6,6 @@ /// import { Config } from '@backstage/config'; -import { CustomErrorBase } from '@backstage/errors'; import { DocumentCollatorFactory } from '@backstage/plugin-search-common'; import { DocumentDecoratorFactory } from '@backstage/plugin-search-common'; import { DocumentTypeInfo } from '@backstage/plugin-search-common'; @@ -115,7 +114,10 @@ export class LunrSearchEngineIndexer extends BatchSearchEngineIndexer { } // @public -export class MissingIndexError extends CustomErrorBase {} +export class MissingIndexError extends Error { + constructor(message?: string, cause?: Error | unknown); + readonly cause?: Error | undefined; +} // @public export class NewlineDelimitedJsonCollatorFactory diff --git a/plugins/search-backend-node/src/errors.ts b/plugins/search-backend-node/src/errors.ts index c6128f3f2e..00b168d460 100644 --- a/plugins/search-backend-node/src/errors.ts +++ b/plugins/search-backend-node/src/errors.ts @@ -14,10 +14,24 @@ * limitations under the License. */ -import { CustomErrorBase } from '@backstage/errors'; +import { isError } from '@backstage/errors'; /** * Failed to query documents for index that does not exist. * @public */ -export class MissingIndexError extends CustomErrorBase {} +export class MissingIndexError extends Error { + /** + * An inner error that caused this error to be thrown, if any. + */ + readonly cause?: Error | undefined; + + constructor(message?: string, cause?: Error | unknown) { + super(message); + + Error.captureStackTrace?.(this, this.constructor); + + this.name = this.constructor.name; + this.cause = isError(cause) ? cause : undefined; + } +} diff --git a/plugins/search-backend/src/service/router.ts b/plugins/search-backend/src/service/router.ts index c85b3ed797..0fbacb018e 100644 --- a/plugins/search-backend/src/service/router.ts +++ b/plugins/search-backend/src/service/router.ts @@ -162,13 +162,7 @@ export async function createRouter( res.send(filterResultSet(toSearchResults(resultSet))); } catch (error) { if (error instanceof MissingIndexError) { - res - .status(400) - .send( - `\nNo index found for types: ${ - query.types ? query.types.join(',') : '' - }. This means there are no documents to search through.`, - ); + res.status(400).json(error.message); } throw new Error(