-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sqlMessage property to Error objects
closes #1714
- Loading branch information
1 parent
5c53e3b
commit 5c60778
Showing
4 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); |