11

I wanted to test my async function querying a table which does not exist. Therefore the error is generated on purpose.

async function getPosts() {
  try {
    const connection = await dbConnection()
    const result = await connection.query('SELECT * FROM x')
    await connection.release()
    console.log(result)
  }
  catch(ex) {
     throw new Error(ex)
  } 
}

When I call that function:

UnhandledPromiseRejectionWarning: Error: Error: ER_NO_SUCH_TABLE: Table 'test.x' doesn't exist.

Can you tell me why?

Fredo Corleone
  • 544
  • 1
  • 6
  • 16

2 Answers2

13

You're getting UnhandledPromiseRejectionWarning because you're not adding a .catch handler to getPosts()


getPosts()
  .then(console.log)
  .catch(console.error); // You're missing this

Or using async/await

try {
    const posts = await getPosts();
    console.log(posts);
} catch(e) { // Missing this
    console.error(e); 
}

There is no need to add a try/catch on your getPosts function if you're going to throw the error again without any modification. Just let it bubble up, and handle the error when calling getPosts() as shown above.

async function getPosts() {

    const connection = await dbConnection()
    const result = await connection.query('SELECT * FROM x')
    await connection.release()
    return result;
}

Regarding your current error, you're trying to perform a query on a table that doesn't exist.

You can learn more about this in the following question: What is an unhandled promise rejection?

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
  • In reality the issue is that I was stupidly throwing a JavaScript error within my `catch` block. – Fredo Corleone Apr 07 '19 at 11:00
  • 3
    Have you read my answer?, I said exactly that, `There is no need to add a try/catch on your getPosts function if you're going to throw the error again without any modification` and it's a bad logic to swallow the error that deep in the code, getPosts should throw if the DB fails, and you should catch it when you call getPosts, not swallow the error. – Marcos Casagrande Apr 07 '19 at 14:15
  • In reality I can't do that, that's part of a route handler and I've simplified it in a standalone function just to ask for help. It has to be a try catch or a any in place error handling because the function call is handled by a framework and it defaults to some internal error catching mechanism. Please remove the downvote from my own answer. – Fredo Corleone Apr 07 '19 at 19:04
  • I won't remove the downvote, because you said the same thing I did in my answer, and accepted your answer, which the exact information. – Marcos Casagrande Apr 07 '19 at 19:06
  • You missed the simple and only fact why my code was throwing unhandled error message. The error handling is fine, but I was stupidly rethrowing the error. I didn't want that behaviour obviously but I was so inexperienced... – Fredo Corleone Apr 07 '19 at 19:08
  • To avoid unhandled promise rejection, you add a `.catch`. Your code is bad and my answer is how to do it correctly, I won't remove the downvote because you scream. And I know what an UnhandledPromiseRejection is, you don't need to lecture me on that, because you were the one who didn't know what they were or how to handle them. – Marcos Casagrande Apr 07 '19 at 19:17
  • To avoid unhandled promise rejection you can either use async/await with try/catch block or chaining a catch to a promise. That's not the faulty behaviour here. The faulty behaviour was caused by a stupid and naive rethrow of the error. I perhaps got that habit by playing with Observable where you catchError(throwError). Anyway I got to thanks for you time and effort. – Fredo Corleone Apr 07 '19 at 19:21
9

Hi myself from the past!

Your console displays an unhandled error because in your catch block you are (I was) stupidly re-throwing a JavaScript error.

Just remove that throw new Error(ex) from your catch and change it with some error handling logic instead.

Fredo Corleone
  • 544
  • 1
  • 6
  • 16