Skip to content

Commit

Permalink
fix(indiekit): add debug logs to troubleshoot the connection to MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdbd authored and paulrobertlloyd committed Aug 24, 2024
1 parent fb832dc commit 1930a39
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/indiekit/lib/mongodb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import makeDebug from "debug";
import { MongoClient } from "mongodb";

const debug = makeDebug(`indiekit:mongodb`);

/**
* Connect to MongoDB client
* @param {string} mongodbUrl - MongoDB URL
Expand All @@ -12,23 +15,26 @@ export const getMongodbClient = async (mongodbUrl) => {
return;
}

// Create client
const connectTimeoutMS = 5000;
try {
debug(`try creating MongoDB client`);
client = new MongoClient(mongodbUrl, {
connectTimeoutMS: 5000,
connectTimeoutMS,
});
} catch (error) {
debug(
`could not create MongoDB client with ${connectTimeoutMS}ms: ${error.message}`,
);
console.error(error.message);

return { error };
}

// Connect to client
try {
debug(`try connecting to MongoDB client`);
await client.connect();
} catch (error) {
debug(`could not connect to MongoDB client: ${error.message}`);
console.error(error.message);

await client.close();
return { error };
}
Expand Down

0 comments on commit 1930a39

Please sign in to comment.