sidorares / sidorares/node-mysql2

named-placeholder exceptions crashes entire app, regardless of catch statement

Open
#986 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
4.4k
Forks
680
Avg merge
9h 7m
Merged PRs (30d)
59

Description

First if all, if this is is better suited for an issue at the named-placeholder repo, let me know and I'll change it.

What happens:
When the named-placeholders module throws an exception when executing a query inside a promise/async function, the error ignores any catch statements and crashes the entire application.

When is happens:
I'm not sure if this happens for all errors, but it happens in the folowing scenario, where I accidentally tried to use an empty array as query parameters.

async function execute(query, params) {
    const formattedQuery = mysql.format(query, params);
    return await pool.execute(formattedQuery);
}

try {
    const query = "REPLACE INTO performance_data SET ?";
    const params = [];
    const response = await execute(query, params);
} catch (error) {
    // Won't get called, app just crashes
    console.error("Error:", error);
}

The full stack trace is:

C:\Users\toblo\Code\performance-app\backend\node_modules\named-placeholders\index.js:95
      throw new Error('Named query contains placeholders, but parameters object is undefined');
      ^

Error: Named query contains placeholders, but parameters object is undefined
    at toArrayParams (C:\Users\toblo\Code\performance-app\backend\node_modules\named-placeholders\index.js:95:13)
    at compile (C:\Users\toblo\Code\performance-app\backend\node_modules\named-placeholders\index.js:144:12)
    at PoolConnection._resolveNamedPlaceholders (C:\Users\toblo\Code\performance-app\backend\node_modules\mysql2\lib\connection.js:518:17)
    at PoolConnection.execute (C:\Users\toblo\Code\performance-app\backend\node_modules\mysql2\lib\connection.js:599:10)
    at C:\Users\toblo\Code\performance-app\backend\node_modules\mysql2\lib\pool.js:163:31
    at C:\Users\toblo\Code\performance-app\backend\node_modules\mysql2\lib\pool.js:44:37
    at processTicksAndRejections (internal/process/task_queues.js:82:9)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the async query example with an empty parameters array, then read named-placeholders/index.js around toArrayParams and compile, followed by mysql2/lib/connection.js and mysql2/lib/pool.js at the stack-trace locations. Determine why the thrown error bypasses the surrounding catch and add a regression test showing that the promise rejects normally and the application does not crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mysql, node.js
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.