Catching a DDP.connect() error

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.

1 Like

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!');
  }
});
2 Likes

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
}
1 Like

thank you,your answer is very good,help me a lot,grateful

1 Like

Thanks for the info, the { retry: false } should be added to the documentation.