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

Add SQL message to error object #1714

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ you spot any mistakes.

## HEAD

* Add `sqlMessage` property to query `Error` objects [#1714](https://github.com/mysqljs/mysql/pull/1714)
* Add `sql` property to query `Error` objects #1462 #1628 #1629
* Update `bignumber.js` to 4.0.2
* Update `readable-stream` to 2.2.9
Expand Down
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,11 @@ 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 associated with the current
SQL State of the database. For example, with
`SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "My Error"`, `err.sqlMessage`
would be set to `"My Error"`. When using triggers for data validation, this
can be used to pass error messages through from the database to the user.

[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
1 change: 1 addition & 0 deletions lib/protocol/sequences/Sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Sequence.prototype._packetToError = function(packet) {
err.code = code;
err.errno = packet.errno;
err.sqlState = packet.sqlState;
err.sqlMessage = packet.message;

return err;
};
Expand Down
46 changes: 46 additions & 0 deletions test/integration/connection/test-error-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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);

// 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], assert.ifError);

// 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 (err) {
// Clean up table if create trigger fails
connection.query('DROP TABLE IF EXISTS ??', [table], assert.ifError);
assert.ifError(err);
});

// Violates trigger condition, so it will throw an error on insert
var row = ['bbbbbbbbbb'];

connection.query('INSERT INTO ?? (name) VALUES ?', [table, [row]], function (insertErr) {
// Remove table when insert finishes
connection.query('DROP TABLE IF EXISTS ??', [table], function (err) {
assert.ifError(err);
connection.end(assert.ifError);
assert.equal(insertErr.sqlMessage, message,
'error sqlMessage property is the trigger error message');
});
});
});