Catching a DDP.connect() error


#1

It seems like if DDP.connect(url) fails it throws an error, but wrapping it in a try/catch doesn’t catch the error. Is there a way to catch that error and handle it appropriately?

I’ve added an HTTP health check endpoint and I’m making a request to that before I make a DDP.connect() call so it should be fine, but I’d like to be able to gracefully handle if the connection doesn’t work.


#2

DDP.connect returns a Connection object with several properties. One of them is status (and it’s reactive). You can check the status of the returned connection object; when it’s set to failed you can then process your desired error handling.

As a quick example (I’m setting retry to false so that if you test this out you can see it fail pretty quickly):

/client/main.js

import { Tracker } from 'meteor/tracker';

const connection = DDP.connect('asdf', { retry: false });
Tracker.autorun(() => {
  if (connection.status().status === 'failed') {
    console.log('Oh no!');
  }
});

#3

Alright that looks like it’d work. Ideally I was really looking for something that runs via a future so that on the server, so I could just do it synchronously without Tracker, like this

const ddpConn = DDP.connect(someUrl);  // runs in a future until connection is set up
if (ddpConn.status === 'failed') {
  // handle failure
}