Skip to content

Commit 20bc5a3

Browse files
jackdbdpaulrobertlloyd
authored andcommitted
feat(indiekit): add debug logs to troubleshoot the connection to MongoDB
1 parent d02b5b8 commit 20bc5a3

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

packages/indiekit/lib/mongodb.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import makeDebug from "debug";
12
import { MongoClient } from "mongodb";
23

4+
const debug = makeDebug(`indiekit:mongodb`);
5+
36
/**
47
* Connect to MongoDB client
58
* @param {string} mongodbUrl - MongoDB URL
@@ -12,23 +15,26 @@ export const getMongodbClient = async (mongodbUrl) => {
1215
return;
1316
}
1417

15-
// Create client
18+
const connectTimeoutMS = 5000;
1619
try {
20+
debug(`try creating MongoDB client`);
1721
client = new MongoClient(mongodbUrl, {
18-
connectTimeoutMS: 5000,
22+
connectTimeoutMS,
1923
});
2024
} catch (error) {
25+
debug(
26+
`could not create MongoDB client with ${connectTimeoutMS}ms: ${error.message}`,
27+
);
2128
console.error(error.message);
22-
2329
return { error };
2430
}
2531

26-
// Connect to client
2732
try {
33+
debug(`try connecting to MongoDB client`);
2834
await client.connect();
2935
} catch (error) {
36+
debug(`could not connect to MongoDB client: ${error.message}`);
3037
console.error(error.message);
31-
3238
await client.close();
3339
return { error };
3440
}

0 commit comments

Comments
 (0)