From 3cde8e19d9e51045e3d9fc43e5f44cf5bef34ede Mon Sep 17 00:00:00 2001 From: ivgo Date: Fri, 3 Jun 2022 15:16:28 +0200 Subject: [PATCH] Use best practices in express responses Signed-off-by: ivgo --- plugins/vault-backend/package.json | 1 + plugins/vault-backend/src/service/VaultBuilder.tsx | 11 ++++------- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/plugins/vault-backend/package.json b/plugins/vault-backend/package.json index 46fa30bf04..1d82786ec0 100644 --- a/plugins/vault-backend/package.json +++ b/plugins/vault-backend/package.json @@ -38,6 +38,7 @@ "@backstage/backend-tasks": "^0.3.2-next.1", "@backstage/backend-test-utils": "^0.1.25-next.1", "@backstage/config": "^1.0.1", + "@backstage/errors": "^1.0.0", "@types/compression": "^1.7.2", "@types/express": "*", "compression": "^1.7.4", diff --git a/plugins/vault-backend/src/service/VaultBuilder.tsx b/plugins/vault-backend/src/service/VaultBuilder.tsx index 20d5eab8e4..8ea2399d84 100644 --- a/plugins/vault-backend/src/service/VaultBuilder.tsx +++ b/plugins/vault-backend/src/service/VaultBuilder.tsx @@ -15,6 +15,7 @@ */ import { Config } from '@backstage/config'; +import { InputError } from '@backstage/errors'; import { Logger } from 'winston'; import express, { Router } from 'express'; import { VaultClient } from './vaultApi'; @@ -147,18 +148,14 @@ export class VaultBuilder { const router = Router(); router.use(express.json()); - router.get('/health', (_, response) => { - response.send({ status: 'ok' }); + router.get('/health', (_, res) => { + res.json({ status: 'ok' }); }); router.get('/v1/secrets', async (req, res) => { const path = req.query.path; if (typeof path !== 'string') { - res - .status(400) - .send('Something was unexpected about the path query string'); - - return; + throw new InputError(`Invalid path: ${path}`); } const secrets = await vaultClient.listSecrets(path);