sequelize: Database connection error handling

Hi there,

I’ve started using Sequelize just recently and one of the things missing for me is a way to detect database connection errors. I can see there is authenticate() function for establishing the initial connection to the db, however I cannot find any events / callbacks for handling errors afterwards - e.g. if the database (postgresql) is restarted my nodejs app simply crashes with the following exception:

events.js:72
        throw er; // Unhandled 'error' event
              ^
error: terminating connection due to administrator command
    at Connection.parseE (/Users/denis/dev/.../node_modules/pg/lib/connection.js:526:11)
    at Connection.parseMessage (/Users/denis/dev/.../node_modules/pg/lib/connection.js:356:17)
    at Socket.<anonymous> (/Users/denis/dev/.../node_modules/pg/lib/connection.js:105:22)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:746:14)
    at Socket.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:408:10)
    at emitReadable (_stream_readable.js:404:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at Socket.Readable.push (_stream_readable.js:127:10)

Is there a workaround for this?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 7
  • Comments: 35 (13 by maintainers)

Most upvoted comments

I’ve been reading the docs looking for a way to listen for connection errors and I was pretty surprised that there’s really no way to do this.

Seems like this should exist:

const sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname');
sequelize.on('error', handleConnectionError);

Would you accept a PR for this?

Is there update on this? is there a way to catch connection error in sequelize and not crash the node app ?

Is the sequelize.on has been added for error handling, or any other way to handle sequelize error, not crash the node app ?

any suggestions on this? really difficult to make correct connection error handling without the capability to track it on sequelize object. Handling such errors on query level is very inconvenient.

What about error which would be raised outside of any query then? You would swallow it and re-throw it when query would be executing? I think that being able to listen to that kind of errors would be cleaner and effortless.

Just like redis client like here: var redisClient = redis .createClient() .on(‘connect’, function () { // handle (all!!) connect event (even when reconnecting after error) }) .on(‘error’, function (error) { // handle EVERY error which would appear during connection });

In my opinion there is a lack of var database = new sequelize(…) .on(‘error’, function(error) { // handle disconnects etc here });

It should be possible to expose “on” method of sequelize database object, no?

This should be re-opened. Any updates on this?

For anyone who needs this, I wrote Keyv which has proper error handling.

It’s only suitable for key-value or cache though, more complex schema needs sequelize or similar.

Is there any further discussion concerning this? I guess you could argue the entire app probably should crash if the DB connection is lost. But I am trying to kill my DB during unit testing and it is throwing an Error in my mocha hook. I’d like a way to handle the gracefully so my tests don’t fail.

The plan is to emit any error from the connection to the query that was executing when we got the error. So if you were doing User.find, that error callback will be called. Preferrably it would be called with an instance of sequelize.ConnectionError, but that is blocked until we find a good way to extend Error in #1971.

With custom error instances in place you’d be able to handle the error accordingly