[backend] use common error types

This commit is contained in:
Fredrik Adelöw
2020-05-04 11:19:23 +02:00
parent 70e2a62155
commit 48b4a86a97
10 changed files with 208 additions and 24 deletions
+1
View File
@@ -21,6 +21,7 @@
"compression": "^1.7.4",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-promise-router": "^3.0.3",
"helmet": "^3.22.0",
"morgan": "^1.10.0",
"winston": "^3.2.1"
@@ -14,19 +14,29 @@
* limitations under the License.
*/
import { NotFoundError } from '@backstage/backend-common';
import { Component, Inventory } from './types';
export class AggregatorInventory implements Inventory {
inventories: Inventory[] = [];
list(): Promise<Component[]> {
return Promise.all(this.inventories.map((i) => i.list())).then((lists) =>
lists.flat(),
);
async list(): Promise<Component[]> {
const lists = await Promise.all(this.inventories.map((i) => i.list()));
return lists.flat();
}
item(id: string): Promise<Component | undefined> {
return this.list().then((items) => items.find((i) => i.id === id));
item(id: string): Promise<Component> {
return new Promise((resolve, reject) => {
const promises = this.inventories.map((i) =>
i.item(id).then(resolve, () => {
// For now, just swallow errors in individual inventories;
// should handle partial errors better
}),
);
Promise.all(promises).then(() =>
reject(new NotFoundError(`Found no component with ID ${id}`)),
);
});
}
enlist(inventory: Inventory) {
@@ -14,16 +14,21 @@
* limitations under the License.
*/
import { NotFoundError } from '@backstage/backend-common';
import { Component, Inventory } from './types';
export class StaticInventory implements Inventory {
constructor(private components: Component[]) {}
list(): Promise<Component[]> {
return Promise.resolve([...this.components]);
async list(): Promise<Component[]> {
return this.components.slice();
}
item(id: string): Promise<Component | undefined> {
return this.list().then((items) => items.find((i) => i.id === id));
async item(id: string): Promise<Component> {
const item = this.components.find((i) => i.id === id);
if (!item) {
throw new NotFoundError(`Found no component with ID ${id}`);
}
return item;
}
}
@@ -19,6 +19,6 @@ export type Component = {
};
export type Inventory = {
list: () => Promise<Component[]>;
item: (id: string) => Promise<Component | undefined>;
list(): Promise<Component[]>;
item(id: string): Promise<Component>;
};
@@ -16,6 +16,7 @@
import express from 'express';
import { Logger } from 'winston';
import Router from 'express-promise-router';
import { Inventory } from '../inventory';
export interface RouterOptions {
@@ -29,7 +30,7 @@ export async function createRouter(
const inventory = options.inventory;
const logger = options.logger.child({ plugin: 'inventory' });
const router = express.Router();
const router = Router();
router
.get('/', async (req, res) => {
const components = await inventory.list();
@@ -38,11 +39,7 @@ export async function createRouter(
.get('/:id', async (req, res) => {
const { id } = req.params;
const component = await inventory.item(id);
if (component) {
res.status(200).send(component);
} else {
res.status(404).send();
}
res.status(200).send(component);
});
const app = express();