Skip to content

Commit

Permalink
Add sqlMessage property to Error objects
Browse files Browse the repository at this point in the history
closes #1714
  • Loading branch information
AdamVig authored and dougwilson committed May 9, 2017
1 parent 5c53e3b commit 5c60778
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ you spot any mistakes.
## HEAD

* Add `sql` property to query `Error` objects #1462 #1628 #1629
* Add `sqlMessage` property to `Error` objects #1714
* Update `bignumber.js` to 4.0.2
* Update `readable-stream` to 2.2.9
* Use `safe-buffer` for improved Buffer API
Expand Down
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,8 @@ object. Additionally they typically come with two extra properties:
* `err.sql`: String, contains the full SQL of the failed query. This can be
useful when using a higher level interface like an ORM that is generating
the queries.
* `err.sqlMessage`: String, contains the message string that provides a
textual description of the error. Only populated from [MySQL server error][].

[Error]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error
[MySQL server error]: http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
Expand Down
4 changes: 3 additions & 1 deletion lib/protocol/sequences/Sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ Sequence.prototype._packetToError = function(packet) {
var err = new Error(code + ': ' + packet.message);
err.code = code;
err.errno = packet.errno;
err.sqlState = packet.sqlState;

err.sqlMessage = packet.message;
err.sqlState = packet.sqlState;

return err;
};
Expand Down
59 changes: 59 additions & 0 deletions test/integration/connection/test-error-sqlmessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var assert = require('assert');
var common = require('../../common');

var table = 'error_message_test';
var message = 'Name must not contain b.';

common.getTestConnection(function (err, connection) {
assert.ifError(err);

common.useTestDb(connection);

createTestTable(function (err3) {
assert.ifError(err3);

// Violates trigger condition, so it will throw an error on insert
connection.query('INSERT INTO ?? (name) VALUES ?', [table, [['bbbbbbbbbb']]], function (err4) {
// Remove table when insert finishes
connection.query('DROP TABLE IF EXISTS ??', [table], function (err5) {
assert.ifError(err5);
assert.ok(err4);
assert.equal(err4.sqlMessage, message, 'error sqlMessage property is the trigger error message');
connection.end(assert.ifError);
});
});
});

function createTestTable(cb) {
// Must use real table because temporary tables cannot have triggers
connection.query([
'CREATE TABLE ?? (',
'`name` varchar(255)',
') ENGINE=InnoDB DEFAULT CHARSET=utf8'
].join('\n'), [table], function (err1) {
if (err1) {
cb(err1);
} else {
// Create a trigger that throws error when name contains the letter "b"
connection.query([
'CREATE TRIGGER `validateName`',
'BEFORE INSERT ON ??',
'FOR EACH ROW BEGIN',
'IF (NEW.name like \'%b%\') THEN',
'SIGNAL SQLSTATE \'45000\' SET MESSAGE_TEXT = ?;',
'END IF;',
'END;'
].join('\n'), [table, message], function (err2) {
if (!err2) {
cb();
} else {
// Clean up table if create trigger fails
connection.query('DROP TABLE IF EXISTS ??', [table], function () {
cb(err2);
});
}
});
}
});
}
});

0 comments on commit 5c60778

Please sign in to comment.