brianRingler / time-distance-tracker

https://time-distance-tracker.vercel.app
0 stars 1 forks source link

API register-user Updates Pushed #14

Open brianRingler opened 11 months ago

brianRingler commented 11 months ago

I made some minor changes and pushed them to the main page. app/api/auth/register-user

The first change was to wrap the checking if the user email already exists in the database. It was not wrapped in a catch block.

  // Check if user email already exists
  try {
    const existingUser = await pool.query(
      "SELECT * FROM user_profiles WHERE user_name_email = $1",
      [email]
    );

    if (existingUser.rows.length > 0) {
      errors.push({ message: "Email already exists" });
    }
  } catch (err) {
    console.log(`Register Page Error when querying the database for email`);
    return new NextResponse({ error: `${err.message}` }, { status: 500 });
  }

So, now both queries to the database are wrapped. To test, you can modify the query to be user_profiless with an extra s which will force an error. Both times the custom error page was pulled up with the option to click and return to home page. app/error-page/page.jsx. Note, I did have to fix the page name as it had a typo.

Couple thoughts:

1) How do we want to handle errors to the database? Right now, the code just has a console log and the user gets directed to an error page. However, if there is a problem and it keeps happening how are "we" being notified and fixing this issue? This really should not be an issue unless the database is down then we have a biigger issue?

2) What is the best way to log errors? Instead of creating just doing a console log to the terminal should be be logging all the errors to a text file? What is the best practice to manage this when we are in production with trillions of users using our application. I think we should create a defined strategy for how to properly handle and log each error. Logging to the console is not good enough as it does not resolve the problem.

imaricodes commented 11 months ago

We should look at a logger library. I haven't had time to read this all, but a good place to start: https://hackernoon.com/the-10-best-nodejs-logging-libraries . I'll follow up when I get a chance to research more