Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More verbose undefined parameter error messages #1707

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ class Connection extends EventEmitter {
if (convertNamedPlaceholders === null) {
convertNamedPlaceholders = require('named-placeholders')();
}
// Store list of named parameters in the order they appear in the sql so we can reference against value array if needed
options.names = options.sql.match(/:\w*/g);
unnamed = convertNamedPlaceholders(options.sql, options.values);
options.sql = unnamed[0];
options.values = unnamed[1];
Expand Down Expand Up @@ -640,25 +642,24 @@ class Connection extends EventEmitter {
'Bind parameters must be array if namedPlaceholders parameter is not enabled'
);
}
options.values.forEach(val => {
//If namedPlaceholder is not enabled and object is passed as bind parameters
if (!Array.isArray(options.values)) {
throw new TypeError(
'Bind parameters must be array if namedPlaceholders parameter is not enabled'
);
}
if (val === undefined) {
throw new TypeError(
'Bind parameters must not contain undefined. To pass SQL NULL specify JS null'
);
if(options.values.some(v => typeof v === 'function')) {
throw new TypeError('Bind parameters must not contain function(s). To pass the body of a function as a string call .toString() first');
}
if (options.names) {
// Create deduped list of parameter names that are undefined by cross referencing the named list with the values array
const undefinedParams = options.names.filter((n, i, a) => a.indexOf(n) === i && options.values[i] === undefined).join(', ');
if(undefinedParams.length) {
throw new TypeError(`Bind parameters must not contain undefined (parameters ${undefinedParams}). To pass SQL NULL specify JS null`);
}
if (typeof val === 'function') {
throw new TypeError(
'Bind parameters must not contain function(s). To pass the body of a function as a string call .toString() first'
);
} else {
// Create list of the indexes of any undefined values
const undefinedIndexes = options.values.reduce((acc, v, i) => v === undefined ? acc.concat(i) : acc, []).join(', ');
if (undefinedIndexes.length) {
throw new TypeError(`Bind parameters must not contain undefined (indexes ${undefinedIndexes}). To pass SQL NULL specify JS null`);
}
});
}
}

const executeCommand = new Commands.Execute(options, cb);
const prepareCommand = new Commands.Prepare(options, (err, stmt) => {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@
"urun": "0.0.8",
"utest": "0.0.8"
}
}
}
56 changes: 56 additions & 0 deletions test/integration/connection/test-execute-undefined-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const createConnection = require('../../common.js').createConnection;
const test = require('utest');
const assert = require('assert');

test('Test error messages for undefined parameters are correctly reported ', {
'Error message lists named parameter that was undefined': () => {
const conn = createConnection({namedPlaceholders: true});
try {
conn.execute({sql: 'select id, email from test_table where id = :id and email = :email and name = :name'}, {email: 'test@email.com'}, err => {
assert.fail(`Expected error to be thrown, but got ${err}`);
});
} catch (err) {
if (err.message.indexOf("(parameters :id, :name)") === -1) {
assert.fail(
`Expected error message to list undefined named parameter (:id):\n ${err}`
);
}
} finally {
conn.end();
}
},
'Error message lists undefined named parameter once if it appears multiple times in the query': () => {
const conn = createConnection({namedPlaceholders: true});
try {
conn.execute({sql: 'select id, email from test_table where id = :id and created < :day and created > :day - interval 7 day'}, {}, err => {
assert.fail(`Expected error to be thrown, but got ${err}`);
});
} catch (err) {
if (err.message.indexOf("(parameters :id, :day)") === -1) {
assert.fail(
`Expected error message to list undefined named parameter (:id):\n ${err}`
);
}
} finally {
conn.end();
}
},
'Error message lists parameter indexes that were undefined': () => {
const conn = createConnection({namedPlaceholders: true});
try {
conn.execute({sql: 'select id, email from test_table where id = ?, email = ?, name = ?'}, [undefined, 'test@test.com', undefined], err => {
assert.fail(`Expected error to be thrown, but got ${err}`);
});
} catch (err) {
if (err.message.indexOf("(indexes 0, 2)") === -1) {
assert.fail(
`Expected error message to list undefined parameter indexes (0,2):\n ${err}`
);
}
} finally {
conn.end();
}
}
});