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.

sql error in streams end in unhandled rejection

See original GitHub issue

When there is an error in a sql query and we return the knex object in order to call .stream() on it, we get the following error :

Unhandled rejection Error: Expected 2 bindings, saw 1
    at replaceRawArrBindings (/data/node_modules/knex/lib/raw.js:181:11)
    at Raw.toSQL (/data/node_modules/knex/lib/raw.js:123:13)
    at Formatter.unwrapRaw (/data/node_modules/knex/lib/formatter.js:91:21)
    at QueryCompiler_MySQL.whereRaw (/data/node_modules/knex/lib/query/compiler.js:541:54)
    at QueryCompiler_MySQL.where (/data/node_modules/knex/lib/query/compiler.js:314:32)
    at /data/node_modules/knex/lib/query/compiler.js:147:30
    at Array.map (native)
    at QueryCompiler_MySQL.select (/data/node_modules/knex/lib/query/compiler.js:146:33)
    at QueryCompiler_MySQL.toSQL (/data/node_modules/knex/lib/query/compiler.js:108:27)
    at Builder.toSQL (/data/node_modules/knex/lib/query/builder.js:111:44)
    at /data/node_modules/knex/lib/runner.js:104:32
    at tryCatcher (/data/node_modules/bluebird/js/release/util.js:16:23)
    at /data/node_modules/bluebird/js/release/using.js:185:26
    at tryCatcher (/data/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/data/node_modules/bluebird/js/release/promise.js:510:31)
    at Promise._settlePromise (/data/node_modules/bluebird/js/release/promise.js:567:18)

This is triggered by any invalid syntax, here we do

const query = knex({
        client:     'mysql',
        connection: {
            host:     '...',
            user:     '...',
            password: '...',
            database: '...',
            port:     '...'
        },
    })
.select('*')
.from(SUBSCRIPTION)
.whereRaw(`DAY(${SUBSCRIPTION}.beginDate) in (?)`, ['30', '31']);

This is an error because of the second parameter passed to whereRaw, which should be [['30', '31']].

The problem is that there seems to be no easy way to properly catch this error.

I tried passing a callback to the .stream() method in order to get a promise back as per the documentation ( http://knexjs.org/#Interfaces-stream ), in hope of getting a rejected promise, but I still get the stream back when doing that, not a promise, so that doesn’t help.

This issue might be related to https://github.com/tgriesser/knex/issues/1852 or https://github.com/tgriesser/knex/issues/948 but I’m not really sure about that.

What I ended doing as a workaround this issue is something like the following:

try {
        query.toString();
    } catch (e) {
        return {
            error: new Error('There is an error in the cursor SQL syntax',
                { errorMessage: e.message }),
        };
    }
return { query };

But that feels rather dirty.

Is there any proper way to catch this kind of error (or any sql syntax error when in stream mode for that matter) ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
mlarchercommented, Jun 7, 2017

yes, no error event triggers

3reactions
mlarchercommented, Nov 10, 2017

Nice catch @rkaw92. That is looking spot on!

I believe it’s better to risk maybe propagating an error twice rather than being sure it is not propagated at all in some cases.

I’m not sure how the maintainer feels about this, considering this issue didn’t get much attention, but it looks like a PR would be in order 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql error in streams end in unhandled rejection #2109 - GitHub
When there is an error in a sql query and we return the knex object in order to call .stream() on it, we...
Read more >
Unhandled promise rejection. This error originated either by ...
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not...
Read more >
2 Server Error Message Reference - MySQL :: Developer Zone
Each server error message includes an error code, SQLSTATE value, ... Message: Unexpected end of file while parsing comment '%s'. Error ... s'...
Read more >
Let It Crash: Best Practices for Handling Node.js Errors on ...
An unhandledRejection error is a newer concept. It is emitted when a promise is not satisfied; in other words, a promise was rejected...
Read more >
SQL Error Codes | SAP Help Portal
Code Type Description 1 WRN_GENERAL general warning 2 ERR_GENERAL general error 3 FATAL_GENERAL fatal error
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