Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
Every thrown error ends up in catch block even tho it has to thrown error before that and stop the rest of the code
    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
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!