不要怂,就是干,撸起袖子干!

Commit 532754f6 by Mick Hansen

Better mysql error handling

1 parent a5b80a77
...@@ -38,8 +38,8 @@ module.exports = (function() { ...@@ -38,8 +38,8 @@ module.exports = (function() {
var self = this var self = this
if (this.useReplicaton) { if (this.useReplicaton) {
var reads = 0, var reads = 0
writes = 0; , writes = 0;
// Init configs with options from config if not present // Init configs with options from config if not present
for (var i in config.replication.read) { for (var i in config.replication.read) {
...@@ -122,7 +122,10 @@ module.exports = (function() { ...@@ -122,7 +122,10 @@ module.exports = (function() {
this.pool = Pooling.Pool({ this.pool = Pooling.Pool({
name: 'sequelize-mysql', name: 'sequelize-mysql',
create: function (done) { create: function (done) {
connect.call(self, done) connect.call(self, function (err, connection) {
// This has to be nested for some reason, else the error won't propagate correctly
done(err, connection);
})
}, },
destroy: function(client) { destroy: function(client) {
disconnect.call(self, client) disconnect.call(self, client)
...@@ -148,8 +151,6 @@ module.exports = (function() { ...@@ -148,8 +151,6 @@ module.exports = (function() {
Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype); Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype);
var isConnecting = false;
ConnectorManager.prototype.query = function(sql, callee, options) { ConnectorManager.prototype.query = function(sql, callee, options) {
if (!this.isConnected && !this.pool) { if (!this.isConnected && !this.pool) {
this.connect() this.connect()
...@@ -163,7 +164,7 @@ module.exports = (function() { ...@@ -163,7 +164,7 @@ module.exports = (function() {
queueItem.query.options.uuid = this.config.uuid queueItem.query.options.uuid = this.config.uuid
enqueue.call(this, queueItem, options) enqueue.call(this, queueItem, options)
return queueItem.query return queueItem.query;
} }
var self = this, query = new Query(this.client, this.sequelize, callee, options || {}); var self = this, query = new Query(this.client, this.sequelize, callee, options || {});
...@@ -282,17 +283,30 @@ module.exports = (function() { ...@@ -282,17 +283,30 @@ module.exports = (function() {
case 'EINVAL': case 'EINVAL':
emitter.emit('error', 'Failed to find MySQL server. Please double check your settings.') emitter.emit('error', 'Failed to find MySQL server. Please double check your settings.')
break break
default:
emitter.emit('error', err);
break;
} }
return;
} }
emitter.emit('success', connection);
}) })
connection.query("SET time_zone = '+0:00'"); connection.query("SET time_zone = '+0:00'");
// client.setMaxListeners(self.maxConcurrentQueries) // client.setMaxListeners(self.maxConcurrentQueries)
this.isConnecting = false this.isConnecting = false
if (config.pool != null && config.pool.handleDisconnects) { if (config.pool !== null && config.pool.handleDisconnects) {
handleDisconnect(this.pool, connection) handleDisconnect(this.pool, connection)
} }
done(null, connection)
emitter.on('error', function (err) {
done(err);
});
emitter.on('success', function (connection) {
done(null, connection);
});
} }
var handleDisconnect = function(pool, client) { var handleDisconnect = function(pool, client) {
......
...@@ -431,7 +431,7 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { ...@@ -431,7 +431,7 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
}) })
}) })
it('fails with incorrect database connection details', function (done) { it('fails with incorrect database credentials (3)', function (done) {
var sequelize = new Sequelize('db', 'user', 'pass', { var sequelize = new Sequelize('db', 'user', 'pass', {
dialect: this.sequelize.options.dialect, dialect: this.sequelize.options.dialect,
port: 99999 port: 99999
...@@ -445,6 +445,22 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { ...@@ -445,6 +445,22 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
done() done()
}) })
}) })
it('fails with incorrect database credentials (4)', function (done) {
var sequelize = new Sequelize('db', 'user', 'pass', {
dialect: this.sequelize.options.dialect,
port: 99999,
pool: {}
});
var Project = sequelize.define('Project', {title: Sequelize.STRING})
var Task = sequelize.define('Task', {title: Sequelize.STRING})
sequelize.sync({force: true}).done(function (err) {
expect(err).to.be.ok
done()
})
})
} }
describe("doesn't emit logging when explicitly saying not to", function() { describe("doesn't emit logging when explicitly saying not to", function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!