sql error in streams end in unhandled rejection
See original GitHub issueWhen 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:
- Created 6 years ago
- Reactions:3
- Comments:7 (3 by maintainers)
yes, no error event triggers
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 😉