Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🪂 fix: Handle MongoDB Connection Errors to Prevent Process Crashes (#11809)

MyGitHub413c2bc0768d
diff --git a/api/db/connect.js b/api/db/connect.js
index 26166ccff8..3534884b57 100644
--- a/api/db/connect.js
+++ b/api/db/connect.js
@@ -40,6 +40,10 @@ if (!cached) {
cached = global.mongoose = { conn: null, promise: null };
}
+mongoose.connection.on('error', (err) => {
+ logger.error('[connectDb] MongoDB connection error:', err);
+});
+
async function connectDb() {
if (cached.conn && cached.conn?._readyState === 1) {
return cached.conn;
diff --git a/api/server/index.js b/api/server/index.js
index fcd0229c9f..193eb423ad 100644
--- a/api/server/index.js
+++ b/api/server/index.js
@@ -251,6 +251,15 @@ process.on('uncaughtException', (err) => {
return;
}
+ if (isEnabled(process.env.CONTINUE_ON_UNCAUGHT_EXCEPTION)) {
+ logger.error('Unhandled error encountered. The app will continue running.', {
+ name: err?.name,
+ message: err?.message,
+ stack: err?.stack,
+ });
+ return;
+ }
+
process.exit(1);
});