You may have noticed that there's a lot of repeated logic in our /books
router.
- Use
router.route
to chain HTTP verbs with optional middleware. This allows us to avoid duplicate route naming and thus typing errors. - Use
router.param
to pull out repeated logic. In this case, whenever anid
is passed in, we'll always send a 404 if the book with that ID does not exist.
Currently we're just calling next(e)
for every error that Prisma throws, but we can use that information to build better errors. Prisma errors have codes, which we can use to do things like attach better status codes.