I'm using express.js with mysql2.
When a duplicate entry error happens on insertion I get the error in the global error handler and that works as expected.
When I log it to the console, the error looks like this:
Error: Duplicate entry 'todd' for key 'users.username_UNIQUE'
at PromisePool.execute (C:\Users\YT\Desktop\PORTFOLIO\MERN\social-talk\server\node_modules\mysql2\promise.js:358:22)
at C:\Users\YT\Desktop\PORTFOLIO\MERN\social-talk\server\models\User.js:45:48 {
code: 'ER_DUP_ENTRY',
errno: 1062,
sqlState: '23000',
sqlMessage: "Duplicate entry 'todd' for key 'users.username_UNIQUE'"
}
My issue is that I'm not sure how to format this error, because it doesn't give the actual field or key that is duplicate (username), but just this sqlMessage field, which is not very good looking to display to the users in this format.
PS: I'm aware of sequelize, but I'm really interested in how I can do this without an ORM