feathersjs-ecosystem / feathers-sync

Synchronize service events between Feathers application instances
MIT License
222 stars 41 forks source link

Server crashes when failing to connect to mongo or when connection is closed #43

Closed petermikitsh closed 6 years ago

petermikitsh commented 7 years ago

Steps to reproduce

Use case 1 / 2:

Create a feathers app and configure the sync module, but do not have your mongo instance running.

import feathers from 'feathers';
import sync from 'feathers-sync';

const app = feathers().configure(sync({db: 'mongodb://localhost:27017/sync'}));
node_modules/mongodb-core/lib/topologies/server.js:321
        return self.emit('error', new MongoError(f('failed to connect to server [%s] on first connect', self.name)));
                                  ^
MongoError: failed to connect to server [localhost:27017] on first connect

Use case 2 / 2:

Kill the mongo server after you've started your feathers application process. Console log below:

node_modules/mongodb-core/lib/error.js:29
    err = new MongoError(options);
          ^
MongoError: connection 0 to localhost:27017 closed

Expected behavior

The server should be able to recover from this behavior. A catchable error should be thrown.

Actual behavior

Since the mubsub client implementation is an isolated implementation detail (https://github.com/feathersjs/feathers-sync/blob/master/src/mongodb.js#L7), I can't add an error hook in my code. So when the app can't connect to mongodb on startup, or if the connection to mongo is closed, the error bubbles up and the process crashes.

System configuration

Tell us about the applicable parts of your setup.

Module versions (especially the part that's not working):

feathers-sync@0.1.2 feathers@2.0.2

NodeJS version: 6.0.0

daffl commented 6 years ago

Better error handling should now be possible by accessing the information available in app.sync:

// For AMQP
app.sync.ready.then(({ connection, channel }) => 
  channel.on('close', () => {});
});

// For MongoDB/Mubsub
app.sync.client.on('error', () => {});