skip to Main Content

i want to handle the probable errors of mysql db. in my case, i have users table that has 7 columns. column email and username values should be unique and i set that for them. but in my sign up form, when users enter and submit their account infos, their entered username and email can be in the database. so in this case, mysql throws an error. for example in my database there is a row with [email protected] email and saman138 username. if a user enters [email protected] for email and saman138 for username, mysql throws an error like this:

Duplicate entry ‘saman138’ for key ‘users.username_UNIQUE’

But the main problem is that i cant display the right error to user. I actually dont know how to do that in the best with the highest performance. For example how can i recognize that the users entered password is duplicated in the database and display the right error to user? I can send two extra queries to get the row that has the entered email or password and then send an error with this message:

your entered username and email already exists in database. Please
enter an other email and username.

this is my codes to insert users infos in to the users table:

import bcrypt from "bcrypt";
import { SignUpInfos } from "../interfaces/Interfaces";
import { mysql } from "../utils/DB";

const signUpUser = async (datas: SignUpInfos) => {
  const hashedPassword = await bcrypt.hash(datas["user-password"], 10);

  const results = await new Promise((resolve, reject) => {
    mysql.query(
      "INSERT INTO users ( fullname, email, username, password ) VALUES ( ?, ?, ?, ? )",
      [
        datas["user-fullname"],
        datas["user-email"],
        datas["user-username"],
        hashedPassword,
      ],
      (err, result) => {
        if (err) reject(err);
        else resolve(result);
      }
    );
  });

  return results;
};

export { signUpUser };

so what is the best way to return the right error message if there was an error? is there any best way to do that or i should send to extra queries? thanks for help 🙂

2

Answers


  1. As according to the MVC pattern, input validation is typically done before the request gets to the database (in your javascript code), not relying on database errors to inform you that something is wrong.

    For example:

    First you might check that a username is a valid username through business rules in the javascript; checking that the username doesnt have spaces or something.
    Then you might search the database to see if there are any users with a given name, return the number and then use that to tell the user that a name is already taken. Only once this search returns that there are no other similar usernames in the database should you actually let them submit the new account.

    But, that is not to say you should abandon the database rules altogether because they are important to ensuring someone doesnt do something dodgy like mess with your database (as an extreme) by bypassing the javascript code somehow and adding duplicate accounts — that would be tragic.

    Where does input validation belong in an MVC application?

    Login or Signup to reply.
  2. "with the highest performance" — "premature optimization". The login process, even when accounting for error cases, takes only milliseconds. There is no need to optimize this.

    Also, since the usual case is "no errors" it is best to assume there will be no errors, then let some lower-level process (such as the databse INSERT) catch the error. If, instead, you checked for errors first, you would usually be wasting your time.

    Anyway, the test and the insert must be done atomically, or else someone else can sneak in between your test and your insert and, say, grab the user_name. That is, the two must be combined into an "atomic" action. And the db, with UNIQUE constraints, does that nicely for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search