feat(search-backend-node): improve error handling
Co-authored-by: Emma Indal <emma.indahl@gmail.com> Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
@@ -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 () => {
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user