Skip to content

[Bug]: Prevent Silent Database Connection Failures #279

@affanraza84

Description

@affanraza84

Bug Description

Description

The database connection logic catches connection errors but does not propagate or handle them appropriately. As a result, the server can start successfully even when MongoDB is unavailable.

Acceptance Criteria

  • Database connection failures prevent server startup.
  • Detailed error information is logged.
  • Application exits with a non-zero status code.
  • Health checks accurately reflect service availability.

Steps to Reproduce

  • Application starts in an unhealthy state.
  • Database-dependent requests fail at runtime.
  • Operational issues become harder to diagnose.
  • Monitoring systems may incorrectly report the service as healthy.

Expected Behavior

The application should fail fast when a database connection cannot be established.

Suggested Solution

Terminate the process when the database connection fails.

catch (error) {
  console.error("Database connection failed:", error);
  process.exit(1);
}

Current Behavior

const connectdb = async () => {
  try {
    await mongoose.connect(process.env.MONGODB_URL);
    console.log("DB connected");
  } catch (_error) {
    console.log("DB error");
  }
};

If the database connection fails, the application continues running and accepts requests despite being unable to access required data.

Environment

MacOS, Safari, Windows, Linux

Logs/Screenshots

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions