0
    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 {
        throw new Error("Something wrong with the database");
    }
}

Wanted result:

  1. If UserId is valid but non-existing in DB throw first error
  2. If UserId is not valid or any other type of error throw second error

Current result:

  1. If UserId is valid but non-existing in DB throw SECOND error
  2. If UserId is not valid or any other type of error throw second error
  • Does this answer your question? [How can I "throw" inside "catch" method from a rejected promise to be handled by an outer "catch" bock?](https://stackoverflow.com/questions/60667147/how-can-i-throw-inside-catch-method-from-a-rejected-promise-to-be-handled-by) – Yogi Jul 11 '22 at 13:38

1 Answers1

2

Well you're throwing 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
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375