Fix error handling for DEL method

This commit is contained in:
Adrian Obelmejias
2016-02-15 18:22:36 -05:00
parent 95f029c3e3
commit 98ab1b8de6
+15 -7
View File
@@ -87,15 +87,23 @@ router.put('/:id',
router.delete('/:id',
ensureUser,
async (ctx) => {
const user = await User.findById(ctx.params.id)
try {
const user = await User.findById(ctx.params.id)
if (!user) {
ctx.throw(404)
}
if (!user) {
ctx.throw(404)
await user.remove()
ctx.body = 200
} catch (err) {
if (err === 404 || err.name === 'CastError') {
ctx.throw(404)
}
ctx.throw(500)
}
await user.remove()
ctx.body = 200
}
)