question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

async pool.execute still throws error although inside try catch

See original GitHub issue

when passing undifiend to pool.execute throws error and node crashes connection.execute works fine and execption is handled

import mysql = require('mysql2'); 
async function x1() {
let pool = await mysql.createPool({
host: 'localhost',user: 'nodejs',
password: 'nodejs', database: 'convfourierDB', 
port: 3306, debug: false })
.promise()
 try {
 await pool.execute('select * from inventory where id=?', [undefined]) 
console.log('x')
 }
 catch (error){
 console.log(error) 
} }
 x1()

when getting connection then execute works fine and execption is handled

async function x1() {
 let pool = await mysql.createPool({
 host: 'localhost', user: 'nodejs',
 password: 'nodejs', database: 'convfourierDB',
 port: 3306, debug: false })
.promise() 
let conn = await pool.getConnection() 
try { 
await conn.execute('select * from inventory where id=?', [undefined]) 
console.log('x') } 
catch (error) { 
console.log(error)
 } } 
x1()

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
sidorarescommented, Feb 8, 2022

the problem is here - https://github.com/sidorares/node-mysql2/blob/dadef383098b0823a90e07fbec18639bd612adfb/lib/pool.js#L177

I think we need to change throw e; into return cb(e); but want to be careful and discuss what that could mean for backwards compatibility. Might require a major release

0reactions
research-and-developcommented, Jul 1, 2022

Hello guys, I just encounter this behavior. Throwing errors from callback functions are simply not catchable and I support the fix using return cb(e).
In my opinion you don’t need major release for such a fix, since this error could only be caught using process.on('uncaughtException' handler or it is going to crash your app otherwise.

I don’t see how it could break any backward code.

It’s good that there is a workaround.

Looking forward for the fix. Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does exception still get thrown after catch in async ...
It doesn't go to the catch because the bad() is terminated by the error. (Even if it weren't, it wouldn't return a promise.)...
Read more >
Async/Await - Best Practices in Asynchronous Programming
When you await a Task, the first exception is re-thrown, so you can catch the specific exception type (such as InvalidOperationException). However, when...
Read more >
Futures and error handling - Dart programming language
Async try -catch-finally using whenComplete() ... If then().catchError() mirrors a try-catch, whenComplete() is the equivalent of 'finally'. The callback ...
Read more >
Don't Block the Event Loop (or the Worker Pool) - Node.js
If a thread is taking a long time to execute a callback (Event Loop) or a task (Worker), we call it "blocked". While...
Read more >
C# Async Antipatterns - Mark Heath
But in most cases, I recommend against using async void . If you can make your method return a Task , you should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found