diff --git a/.changeset/seven-swans-glow.md b/.changeset/seven-swans-glow.md index 8a7a6eab76..2d41b94082 100644 --- a/.changeset/seven-swans-glow.md +++ b/.changeset/seven-swans-glow.md @@ -1,7 +1,7 @@ --- -'@backstage/plugin-tech-insights': minor -'@backstage/plugin-tech-insights-backend': minor -'@backstage/plugin-tech-insights-common': minor +'@backstage/plugin-tech-insights': patch +'@backstage/plugin-tech-insights-backend': patch +'@backstage/plugin-tech-insights-common': patch '@backstage/plugin-tech-insights-backend-module-jsonfc': patch --- diff --git a/plugins/tech-insights-backend-module-jsonfc/src/service/JsonRulesEngineFactChecker.ts b/plugins/tech-insights-backend-module-jsonfc/src/service/JsonRulesEngineFactChecker.ts index 333744a203..8866b5badb 100644 --- a/plugins/tech-insights-backend-module-jsonfc/src/service/JsonRulesEngineFactChecker.ts +++ b/plugins/tech-insights-backend-module-jsonfc/src/service/JsonRulesEngineFactChecker.ts @@ -35,6 +35,7 @@ import { pick } from 'lodash'; import Ajv, { SchemaObject } from 'ajv'; import * as validationSchema from './validation-schema.json'; import { JSON_RULE_ENGINE_CHECK_TYPE } from '../constants'; +import { isError } from '@backstage/errors'; const noopEvent = { type: 'noop', @@ -138,8 +139,11 @@ export class JsonRulesEngineFactChecker techInsightChecks, Object.values(facts), ); - } catch (e: any) { - throw new Error(`Failed to run rules engine, ${e.message}`); + } catch (e) { + if (isError(e)) { + throw new Error(`Failed to run rules engine, ${e.message}`); + } + throw e; } } diff --git a/plugins/tech-insights-backend/src/service/router.ts b/plugins/tech-insights-backend/src/service/router.ts index dd89fefed8..22a80c2d79 100644 --- a/plugins/tech-insights-backend/src/service/router.ts +++ b/plugins/tech-insights-backend/src/service/router.ts @@ -32,6 +32,7 @@ import { parseEntityName, stringifyEntityRef, } from '@backstage/catalog-model'; +import { errorHandler } from '@backstage/backend-common'; /** * @public @@ -92,36 +93,26 @@ export async function createRouter< router.post('/checks/run/:namespace/:kind/:name', async (req, res) => { const { namespace, kind, name } = req.params; - try { - const { checks }: { checks: string[] } = req.body; - const entityTriplet = stringifyEntityRef({ namespace, kind, name }); - const checkResult = await factChecker.runChecks(entityTriplet, checks); - return res.send(checkResult); - } catch (e: any) { - return res.status(500).json({ message: e.message }).send(); - } + const { checks }: { checks: string[] } = req.body; + const entityTriplet = stringifyEntityRef({ namespace, kind, name }); + const checkResult = await factChecker.runChecks(entityTriplet, checks); + return res.send(checkResult); }); router.post('/checks/run', async (req, res) => { - try { - const { - checks, - entities, - }: { checks: string[]; entities: EntityName[] } = req.body; - const tasks = entities.map(async entity => { - const entityTriplet = - typeof entity === 'string' ? entity : stringifyEntityRef(entity); - const results = await factChecker.runChecks(entityTriplet, checks); - return { - entity: entityTriplet, - results, - }; - }); - const results = await Promise.all(tasks); - return res.send(results); - } catch (e: any) { - return res.status(500).json({ message: e.message }).send(); - } + const { checks, entities }: { checks: string[]; entities: EntityName[] } = + req.body; + const tasks = entities.map(async entity => { + const entityTriplet = + typeof entity === 'string' ? entity : stringifyEntityRef(entity); + const results = await factChecker.runChecks(entityTriplet, checks); + return { + entity: entityTriplet, + results, + }; + }); + const results = await Promise.all(tasks); + return res.send(results); }); } else { logger.info( @@ -176,5 +167,7 @@ export async function createRouter< ), ); }); + + router.use(errorHandler()); return router; }