From 8e63e71b2cb02a23c509fbe62b2d4edd6f7ea4d5 Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Tue, 28 Jun 2022 11:45:10 +0200 Subject: [PATCH] feat(search-backend-node): improve error handling Co-authored-by: Emma Indal Signed-off-by: Camila Belo --- .../src/engines/LunrSearchEngine.test.ts | 13 +++++ .../src/engines/LunrSearchEngine.ts | 53 +++++++++++-------- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/plugins/search-backend-node/src/engines/LunrSearchEngine.test.ts b/plugins/search-backend-node/src/engines/LunrSearchEngine.test.ts index 098d1e8b16..0bc2e223a6 100644 --- a/plugins/search-backend-node/src/engines/LunrSearchEngine.test.ts +++ b/plugins/search-backend-node/src/engines/LunrSearchEngine.test.ts @@ -949,6 +949,19 @@ describe('LunrSearchEngine', () => { previousPageCursor: undefined, }); }); + + it('should throws missing index error', async () => { + await expect( + async () => + await testLunrSearchEngine.query({ + term: 'testTerm', + types: ['unknown'], + filters: {}, + }), + ).rejects.toThrow( + 'Missing index for unknown. This means there are no documents to search through', + ); + }); }); it('should return previous page cursor if on another page', async () => { diff --git a/plugins/search-backend-node/src/engines/LunrSearchEngine.ts b/plugins/search-backend-node/src/engines/LunrSearchEngine.ts index 03cb421653..fcf30f1844 100644 --- a/plugins/search-backend-node/src/engines/LunrSearchEngine.ts +++ b/plugins/search-backend-node/src/engines/LunrSearchEngine.ts @@ -21,6 +21,7 @@ import { QueryTranslator, SearchEngine, } from '@backstage/plugin-search-common'; +import { MissingIndexError } from '../errors'; import lunr from 'lunr'; import { v4 as uuid } from 'uuid'; import { Logger } from 'winston'; @@ -163,30 +164,38 @@ export class LunrSearchEngine implements SearchEngine { const results: LunrResultEnvelope[] = []; + const indexKeys = Object.keys(this.lunrIndices).filter( + type => !documentTypes || documentTypes.includes(type), + ); + + if (!indexKeys.length) { + throw new MissingIndexError( + `Missing index for ${documentTypes?.toString()}. This means there are no documents to search through.`, + ); + } + // Iterate over the filtered list of this.lunrIndex keys. - Object.keys(this.lunrIndices) - .filter(type => !documentTypes || documentTypes.includes(type)) - .forEach(type => { - try { - results.push( - ...this.lunrIndices[type].query(lunrQueryBuilder).map(result => { - return { - result: result, - type: type, - }; - }), - ); - } catch (err) { - // if a field does not exist on a index, we can see that as a no-match - if ( - err instanceof Error && - err.message.startsWith('unrecognised field') - ) { - return; - } - throw err; + indexKeys.forEach(type => { + try { + results.push( + ...this.lunrIndices[type].query(lunrQueryBuilder).map(result => { + return { + result: result, + type: type, + }; + }), + ); + } catch (err) { + // if a field does not exist on a index, we can see that as a no-match + if ( + err instanceof Error && + err.message.startsWith('unrecognised field') + ) { + return; } - }); + throw err; + } + }); // Sort results. results.sort((doc1, doc2) => {