Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Mongoose Connection Failures Gracefully #24

Open
AyushDharDubey opened this issue Dec 17, 2024 · 5 comments · May be fixed by #67
Open

Handle Mongoose Connection Failures Gracefully #24

AyushDharDubey opened this issue Dec 17, 2024 · 5 comments · May be fixed by #67
Assignees
Labels
bug Something isn't working medium

Comments

@AyushDharDubey
Copy link
Collaborator

Description

Mongoose fails to connect to the database, the application does not currently handle the error, causing the app to crash unexpectedly. One common reason is that you're trying to access the database from an IP that isn't whitelisted. This leads to poor user experience on the frontend and unclear debugging information on the backend. To address this, we need to implement both frontend and backend improvements to ensure meaningful feedback, error handling and stability during database outages.


Steps to Reproduce

  1. Stop the MongoDB server/use an invalid database URI/remove your current ip address from network access list on security tab of mongodb console.
  2. Start your local server.
  3. Try to access the /api/Tickets endpoint via the frontend.
  4. Observe that the error is not handled effectively on either of the backend and frontend and the app breaks.

Proposed Solutions

1. Backend Enhancements

  • Handle Database Connection Errors Gracefully:
    • Wrap the Mongoose connection logic in a try-catch block and handle connection errors.
    • Provide meaningful HTTP responses to the client when a database connection fails (e.g., return 503 Service Unavailable).

2. Frontend Enhancements

  • Graceful Fallback UI:
    Show a user-friendly message when the backend is unable to fetch data due to database connection issues.

    // app/page.jsx
    
    const data = await getTickets();
    
    if (!data || !data?.tickets) {
      return (
        <div className="p-5">
          <h2 className="text-red-500">
            Unable to load tickets. Please try again later.
          </h2>
        </div>
      );
    }
    
    const tickets = data.tickets;
@AyushDharDubey AyushDharDubey added bug Something isn't working hard medium and removed hard labels Dec 17, 2024
@anshkarwasra
Copy link
Collaborator

okay im working on this issue for now and will update if any progress is made

@anshkarwasra
Copy link
Collaborator

anshkarwasra commented Dec 19, 2024

can i ask you where is the .env file from which mongouri is loading ( whenever i reload the application the mongouri variable becomes undefined)

@AyushDharDubey
Copy link
Collaborator Author

can i ask you where is the .env file from which mongouri is loading ( whenever i reload the application the mongouri variable becomes undefined)

the .env file can not be included in the codebase for security reasons, users/contributors are required to register themselves on MongoDB Atlas, setup a new cluster and add their own connection uri in .env.local file created by themselves on their local system.

refer to README.md of the repo for more info

@anshkarwasra
Copy link
Collaborator

anshkarwasra commented Dec 20, 2024

okay made some final changes and some refactoring in the code and provided adequate responses for every situation (like if tickets array is empty or db connection was not successfull) here are some key changes:-

change #1

made db connection as a async function in a seperate js file called db.js in models dir

  • Reason
  1. to stop potential buffring timeout :- earlier when db connection was in Tickets.js and if anyone tries to connect with bad credentials to the db the connection wasn't made that was handled but in /api/route.js const tickets = await Ticket.find(); was causing buffring timeout
    buffring

so by awaiting connection to db stopped this potential timeout error
con

change #2

provided different responses for different situations as connection timeout , tickets are empty ,unable to connect to db or bad

change #3

chnaged front end accordingly displayed hmm.. Unable to load tickets. Please try again later. if unable to featch tickets or tickets are empty or in case of internal server error

change #4

reverted the changes you mentioned yesterday

@AyushDharDubey
Copy link
Collaborator Author

Hi @anshkarwasra, so far it seems good enough, please push the changes so that i can better understand the implementation and we can proceed further

@anshkarwasra anshkarwasra linked a pull request Dec 27, 2024 that will close this issue
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working medium
Projects
None yet
2 participants