Well you're throw
ing an exception inside a try
block, which will be caught, and your catch
block then re-throws a different exception. You can either inspect the caught exception
async deleteUser(userId: string): Promise<boolean> {
try {
const userToDelete = await userModel.findByIdAndDelete(userId)
if (!userToDelete) {
throw new Error(`User with id: ${userId} does not exist`)
}
return true
} catch(e) {
if (e.message == `User with id: ${userId} does not exist`) throw e
else throw new Error("Something wrong with the database")
}
}
(Checking an e.code
you put on the error with Object.assign
, or testing for an Error
subclass with instanceof
, would be nicer than testing the message)
or put the try
closer around the await …
statement whose errors you want to handle:
async deleteUser(userId: string): Promise<boolean> {
let userToDelete
try {
userToDelete = await userModel.findByIdAndDelete(userId)
} catch {
throw new Error("Something wrong with the database");
}
if (!userToDelete) {
throw new Error(`User with id: ${userId} does not exist`)
}
return true
}
which is nicer with .catch()
:
async deleteUser(userId: string): Promise<boolean> {
const userToDelete = await userModel.findByIdAndDelete(userId).catch(e => {
throw new Error("Something wrong with the database");
})
if (!userToDelete) {
throw new Error(`User with id: ${userId} does not exist`)
}
return true
}