refactor: apply review suggestions

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2022-07-04 15:52:18 +02:00
parent faa5dde25e
commit dcba440459
7 changed files with 9 additions and 34 deletions
-1
View File
@@ -1,6 +1,5 @@
---
'@backstage/plugin-search-backend-module-elasticsearch': patch
'@backstage/plugin-search-backend-module-pg': patch
---
Throws `MissingIndexError` when no index of type exist.
@@ -813,7 +813,7 @@ describe('ElasticSearchSearchEngine', () => {
filters: {},
}),
).rejects.toThrow(
'Missing index for unknown__search. This means there are no documents to search through',
'Missing index for unknown__search. This means there are no documents to search through.',
);
});
});
@@ -271,21 +271,6 @@ describe('PgSearchEngine', () => {
limit: 26,
});
});
it('should throws missing index error', async () => {
database.transaction.mockImplementation(fn => fn(tx));
database.query.mockResolvedValue([]);
await expect(
async () =>
await searchEngine.query({
term: 'testTerm',
types: ['unknown'],
filters: {},
}),
).rejects.toThrow(
'Missing index for unknown. This means there are no documents to search through',
);
});
});
});
@@ -15,7 +15,6 @@
*/
import { PluginDatabaseManager } from '@backstage/backend-common';
import { SearchEngine } from '@backstage/plugin-search-backend-node';
import { MissingIndexError } from '@backstage/plugin-search-backend-node';
import {
SearchQuery,
IndexableResultSet,
@@ -93,12 +92,6 @@ export class PgSearchEngine implements SearchEngine {
this.databaseStore.query(tx, pgQuery),
);
if (pgQuery?.types && !rows.length) {
throw new MissingIndexError(
`Missing index for ${pgQuery.types}. This means there are no documents to search through.`,
);
}
// We requested one result more than the page size to know whether there is
// another page.
const { page } = decodePageCursor(query.pageCursor);
@@ -959,7 +959,7 @@ describe('LunrSearchEngine', () => {
filters: {},
}),
).rejects.toThrow(
'Missing index for unknown. This means there are no documents to search through',
"Missing index for unknown. This could be because the index hasn't been created yet or there was a problem during index creation.",
);
});
});
@@ -170,7 +170,7 @@ export class LunrSearchEngine implements SearchEngine {
if (documentTypes?.length && !indexKeys.length) {
throw new MissingIndexError(
`Missing index for ${documentTypes?.toString()}. This means there are no documents to search through.`,
`Missing index for ${documentTypes?.toString()}. This could be because the index hasn't been created yet or there was a problem during index creation.`,
);
}
+6 -8
View File
@@ -19,7 +19,7 @@ import Router from 'express-promise-router';
import { Logger } from 'winston';
import { z } from 'zod';
import { errorHandler } from '@backstage/backend-common';
import { InputError } from '@backstage/errors';
import { ErrorResponseBody, InputError } from '@backstage/errors';
import { Config } from '@backstage/config';
import { JsonObject, JsonValue } from '@backstage/types';
import { getBearerTokenFromAuthorizationHeader } from '@backstage/plugin-auth-node';
@@ -33,10 +33,7 @@ import {
IndexableResultSet,
SearchResultSet,
} from '@backstage/plugin-search-common';
import {
SearchEngine,
MissingIndexError,
} from '@backstage/plugin-search-backend-node';
import { SearchEngine } from '@backstage/plugin-search-backend-node';
import { AuthorizedSearchEngine } from './AuthorizedSearchEngine';
const jsonObjectSchema: z.ZodSchema<JsonObject> = z.lazy(() => {
@@ -134,7 +131,7 @@ export async function createRouter(
'/query',
async (
req: express.Request,
res: express.Response<SearchResultSet | string>,
res: express.Response<SearchResultSet | ErrorResponseBody>,
) => {
const parseResult = requestSchema.safeParse(req.query);
@@ -161,8 +158,9 @@ export async function createRouter(
res.send(filterResultSet(toSearchResults(resultSet)));
} catch (error) {
if (error instanceof MissingIndexError) {
res.status(400).json(error.message);
if (error.name === 'MissingIndexError') {
// re-throw and let the default error handler middleware captures it and serializes it with the right response code on the standard form
throw error;
}
throw new Error(