don't try to use db connection if error is thrown

Currently, if there are issues connecting to mongo
db.migrationCollection is still called. If db is undefined, this will
raise an exception. Moving error conditional up, so that an error is
logged instead.