catalog-backend: use more explicit response types and let error handlers handle errors

This commit is contained in:
Patrik Oldsberg
2021-02-08 13:20:45 +01:00
parent 82af0855bc
commit 3c4444cea2
+16 -20
View File
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { errorHandler } from '@backstage/backend-common';
import { errorHandler, NotFoundError } from '@backstage/backend-common';
import {
locationSpecSchema,
analyzeLocationSchema,
@@ -57,7 +57,7 @@ export async function createRouter(
const filter = EntityFilters.ofQuery(req.query);
const fieldMapper = translateQueryToFieldMapper(req.query);
const entities = await entitiesCatalog.entities(filter);
res.status(200).send(entities.map(fieldMapper));
res.status(200).json(entities.map(fieldMapper));
})
.post('/entities', async (req, res) => {
const body = await requireRequestBody(req);
@@ -67,7 +67,7 @@ export async function createRouter(
const [entity] = await entitiesCatalog.entities(
EntityFilters.ofMatchers({ 'metadata.uid': result.entityId }),
);
res.status(200).send(entity);
res.status(200).json(entity);
})
.get('/entities/by-uid/:uid', async (req, res) => {
const { uid } = req.params;
@@ -75,15 +75,14 @@ export async function createRouter(
EntityFilters.ofMatchers({ 'metadata.uid': uid }),
);
if (!entities.length) {
res.status(404).send(`No entity with uid ${uid}`);
} else {
res.status(200).send(entities[0]);
throw new NotFoundError(`No entity with uid ${uid}`);
}
res.status(200).json(entities[0]);
})
.delete('/entities/by-uid/:uid', async (req, res) => {
const { uid } = req.params;
await entitiesCatalog.removeEntityByUid(uid);
res.status(204).send();
res.status(204).end();
})
.get('/entities/by-name/:kind/:namespace/:name', async (req, res) => {
const { kind, namespace, name } = req.params;
@@ -95,14 +94,11 @@ export async function createRouter(
}),
);
if (!entities.length) {
res
.status(404)
.send(
`No entity with kind ${kind} namespace ${namespace} name ${name}`,
);
} else {
res.status(200).send(entities[0]);
throw new NotFoundError(
`No entity with kind ${kind} namespace ${namespace} name ${name}`,
);
}
res.status(200).json(entities[0]);
});
}
@@ -111,7 +107,7 @@ export async function createRouter(
const input = await validateRequestBody(req, locationSpecSchema);
const dryRun = yn(req.query.dryRun, { default: false });
const output = await higherOrderOperation.addLocation(input, { dryRun });
res.status(201).send(output);
res.status(201).json(output);
});
}
@@ -119,22 +115,22 @@ export async function createRouter(
router
.get('/locations', async (_req, res) => {
const output = await locationsCatalog.locations();
res.status(200).send(output);
res.status(200).json(output);
})
.get('/locations/:id/history', async (req, res) => {
const { id } = req.params;
const output = await locationsCatalog.locationHistory(id);
res.status(200).send(output);
res.status(200).json(output);
})
.get('/locations/:id', async (req, res) => {
const { id } = req.params;
const output = await locationsCatalog.location(id);
res.status(200).send(output);
res.status(200).json(output);
})
.delete('/locations/:id', async (req, res) => {
const { id } = req.params;
await locationsCatalog.removeLocation(id);
res.status(204).send();
res.status(204).end();
});
}
@@ -142,7 +138,7 @@ export async function createRouter(
router.post('/analyze-location', async (req, res) => {
const input = await validateRequestBody(req, analyzeLocationSchema);
const output = await locationAnalyzer.analyzeLocation(input);
res.status(200).send(output);
res.status(200).json(output);
});
}