simplify error handling

Signed-off-by: goenning <me@goenning.net>
This commit is contained in:
goenning
2022-01-11 12:22:48 +00:00
parent a60eb0f0dd
commit 60ec1a959b
3 changed files with 29 additions and 32 deletions
+3 -3
View File
@@ -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
---
@@ -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;
}
}
@@ -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;
}