CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 64 forks source link

Running a query with pool that expects an error does not throw an error in result. #163

Closed JadenSWang closed 2 years ago

JadenSWang commented 2 years ago

The title says it all and perhaps I'm misunderstanding the return of the query function but how do I catch and handle the errors thrown my the server?

CodeFoodPixels commented 2 years ago

Can you give me some example code that causes the issue?

JadenSWang commented 2 years ago

`import { initializePool } from "./init.db";

export async function createUser(user_id: String) { const connectionPool = await initializePool()

const res = await connectionPool.query('CALL prodInitializeNewUser(?)', [user_id]);
return res;

}`

The prodInitializeNewUser procedure just signals an error.

And I break down the result with: const [data, fields, query] = res

Am I misunderstanding something here?

CodeFoodPixels commented 2 years ago

It should be throwing any errors, but it won't be in res, because it'll reject the promise. As you're using await you want to do something like this with try/catch.

import { initializePool } from "./init.db";

export async function createUser(user_id: String) {
const connectionPool = await initializePool()

try {
  const res = await connectionPool.query('CALL prodInitializeNewUser(?)', [user_id]);
  return res;
} catch (e) {
  console.log(e)
}

}
JadenSWang commented 2 years ago

Perhaps there's something wrong with my procedure then, I'll take a look, thanks!