title: Handle database error when using connect-mongo
body: <p>I've got a fairly standard connect-mongo setup </p>
  
  <p><em>mongoose is initialised / connected prior to this</em></p>
  
  <pre><code>app.use(express.session({
      secret: "sfdgsdgsdfg",
      store: new MongoSessionStore({db: mongoose.connection.db})
  }));
  </code></pre>
  
  <p>This works fine.</p>
  
  <p>However -
  Assuming my mongodb connection suddenly dies (stop mongod locally in the example below)
   - the next time I try to hit a route, my express app crashes too -</p>
  
  <blockquote>
    <p>Error: failed to connect to [localhost:27017]
        at null. (/Users/alex/Projects/MyProject/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74)
        at emit (events.js:106:17)
        at null. (/Users/alex/Projects/MyProject/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
        at emit (events.js:98:17)
        at Socket. (/Users/alex/Projects/MyProject/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
        at Socket.emit (events.js:95:17)
        at net.js:440:14
        at process._tickCallback (node.js:419:13)</p>
  </blockquote>
  
  <p>Is there a way to handle this error and (For example) redirect to a /error route?
  (Obviously one that doesn't require session!)</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>So now, I'm creating a separate mongoose connection.</p>
  
  <p>I then use the <code>on('error'</code> to listen for errors</p>
  
  <p>...this is where I'm getting stuck -
  The process still dies because re-throwing the err doesn't pass it into the express error handler...</p>
  
  <pre><code>var sessionDbConnection = mongoose.createConnection(config.sessionDb);
  
  sessionDbConnection.on('error', function(err) {
      console.log('oops');
      throw err; //instead of re-throwing the error, i think i need to pass it to the error handler below??
  })
  
  app.use(express.session({
      secret: "sfdgsdgsdfg",
      store: new MongoSessionStore({db: sessionDbConnection.db})
  }));
  
  app.use(function(err, req, res, next) {
      res.render('error', err); //just for testing
  });
  </code></pre>