Skip to content

Commit

Permalink
feat(1.2c): Send error responses in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
one-aalam committed Jul 14, 2021
1 parent 980246b commit e91496f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/routes/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export default async function categories(fastify: FastifyInstance) {
}>('/categories/:id', async (req, reply) => {
const recipeCategory = RECIPE_CATEGORIES.find(recipeCategory => recipeCategory.id === parseInt(req.params.id))
if(!recipeCategory) {
reply.code(404).type('text/html').send('Not Found')
reply.code(404).send({
name: 'NotFoundError',
message: 'Not Found'
})
}
return recipeCategory
})
Expand Down
5 changes: 4 additions & 1 deletion src/routes/recipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export default async function recipes(fastify: FastifyInstance) {
}>('/recipes/:id', async (req, reply) => {
const recipe = RECIPES.find(recipe => recipe.id === parseInt(req.params.id))
if(!recipe) {
reply.code(404).type('text/html').send('Not Found')
reply.code(404).send({
name: 'NotFoundError',
message: 'Not Found'
})
}
return recipe
})
Expand Down
5 changes: 4 additions & 1 deletion src/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export default async function users(fastify: FastifyInstance) {
}>('/users/:id', async (req, reply) => {
const user = USERS.find(user => user.id === parseInt(req.params.id))
if(!user) {
reply.code(404).type('text/html').send('Not Found')
reply.code(404).send({
name: 'NotFoundError',
message: 'Not Found'
})
}
return user
})
Expand Down

0 comments on commit e91496f

Please sign in to comment.