# Handling errors in mysql

**URL:** <https://discourse.nodered.org/t/handling-errors-in-mysql/95973>\
**Category:** General\
**Tags:** mysql\
**Created:** [13 March 2025 09:47 UTC](https://discourse.nodered.org/t/handling-errors-in-mysql/95973 "2025-03-13T09:47:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![apena](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@apena](https://discourse.nodered.org/u/apena)\
**Post date:** [13 March 2025 09:47 UTC](https://discourse.nodered.org/t/handling-errors-in-mysql/95973/1 "2025-03-13T09:47:12Z")

</div>

I have a MySQL database with a column `email` that is unique. I have an endpoint to insert data into the database. When I try to create a user with a duplicate email, I don't know how to get the `ER_DUP_ENTRY` error code that MySQL returns. I have a `catch` where I capture the error, but when I display the error with the debug node, it shows the entire message, and I want to extract only the `ER_DUP_ENTRY` error code.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 March 2025 09:49 UTC](https://discourse.nodered.org/t/handling-errors-in-mysql/95973/2 "2025-03-13T09:49:04Z")

</div>

> [@apena](#):
>
> I want to extract only the `ER_DUP_ENTRY` error cod

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path/value for any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![apena](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@apena](https://discourse.nodered.org/u/apena)\
**Post date:** [13 March 2025 09:56 UTC](https://discourse.nodered.org/t/handling-errors-in-mysql/95973/3 "2025-03-13T09:56:36Z")

</div>

The problem is that the object msg.error does not include an error code.

---

<div class="post-metadata">

**Author:** ![apena](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@apena](https://discourse.nodered.org/u/apena)\
**Post date:** [13 March 2025 09:58 UTC](https://discourse.nodered.org/t/handling-errors-in-mysql/95973/4 "2025-03-13T09:58:17Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/ab0ecd66661b21ae6851a8ec7ea6a4cb960141d4.png)  
So I can't access the error code `ER_DUP_ENTRY`  
If I were using Node, I could access the error code:

```auto
connection.on('error', (err) => {
  console.error('Database error:', err.message);

  // Handle connection lost error
  if (err.code === 'ER_DUP_ENTRY') {
    console.error('duplicated email.');    
  } else {
    throw err;
  }
});

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 March 2025 10:24 UTC](https://discourse.nodered.org/t/handling-errors-in-mysql/95973/5 "2025-03-13T10:24:53Z")

</div>

You could always send the output of the `catch` node to a switch node and  
have the 'Property' set to 'msg.error.message'  
select the `contains` option and enter "Error: Duplicate entry  
and have another option using `otherwise` to send the valid data onward.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [13 March 2025 11:46 UTC](https://discourse.nodered.org/t/handling-errors-in-mysql/95973/6 "2025-03-13T11:46:30Z")

</div>

> [@apena](#):
>
> I don't know how to get the `ER_DUP_ENTRY` error code that MySQL returns.

The actual error code is 1062.  
Node-red helpfully (?) translates this for you to `ER_DUP_ENTRY` using a lookup table at ~/.node-red/.node\_modules/mysql2/lib/constants/errors.js:exports.  
Of course it fails to include either the original code or the shortened string in the message properties (which you might consider a bug worth reporting).

You can obtain the error code from the database with this flow. It only shows the error from the most recent query executed so it relies on nothing else happening in the db between the two queries.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d2a0a804bec0893ff4e70937ecc7ce81fa2665f.png)

```auto
[{"id":"cca57a9f7b5c6572","type":"group","z":"4b8cd70fedd40617","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["529b71b83bd3e665","ad2c96e4ac6b8c3e","fa714713423592b3","17ec365e3d6b338a","f4b371be87419697","c31973f08b83ba7c"],"x":234,"y":79,"w":652,"h":142},{"id":"529b71b83bd3e665","type":"inject","z":"4b8cd70fedd40617","g":"cca57a9f7b5c6572","name":"Make an error","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"insert into users(id, username) values (1, \"X Billups\")","x":350,"y":120,"wires":[["ad2c96e4ac6b8c3e"]]},{"id":"ad2c96e4ac6b8c3e","type":"mysql","z":"4b8cd70fedd40617","g":"cca57a9f7b5c6572","mydb":"2381cb15.b563bc","name":"","x":520,"y":120,"wires":[[]]},{"id":"fa714713423592b3","type":"catch","z":"4b8cd70fedd40617","g":"cca57a9f7b5c6572","name":"","scope":["ad2c96e4ac6b8c3e"],"uncaught":false,"x":310,"y":180,"wires":[["17ec365e3d6b338a"]]},{"id":"17ec365e3d6b338a","type":"change","z":"4b8cd70fedd40617","g":"cca57a9f7b5c6572","name":"topic = show warnings","rules":[{"t":"set","p":"topic","pt":"msg","to":"show warnings","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":180,"wires":[["f4b371be87419697"]]},{"id":"f4b371be87419697","type":"mysql","z":"4b8cd70fedd40617","g":"cca57a9f7b5c6572","mydb":"2381cb15.b563bc","name":"","x":660,"y":180,"wires":[["c31973f08b83ba7c"]]},{"id":"c31973f08b83ba7c","type":"debug","z":"4b8cd70fedd40617","g":"cca57a9f7b5c6572","name":"Show","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":180,"wires":[]},{"id":"2381cb15.b563bc","type":"MySQLdatabase","name":"espresso","host":"127.0.0.1","port":"3306","db":"espresso","tz":"","charset":"UTF8"}]

```

---

<div class="post-metadata">

**Author:** ![apena](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@apena](https://discourse.nodered.org/u/apena)\
**Post date:** [13 March 2025 12:00 UTC](https://discourse.nodered.org/t/handling-errors-in-mysql/95973/7 "2025-03-13T12:00:25Z")

</div>

Thanks everyone for contributing! I've created an issue describing the problem in the Node-red repository [Error object doesn't include code attribute · Issue #5080 · node-red/node-red · GitHub](https://github.com/node-red/node-red/issues/5080)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [13 March 2025 12:08 UTC](https://discourse.nodered.org/t/handling-errors-in-mysql/95973/8 "2025-03-13T12:08:18Z")

</div>

Ah. I see that issue was raised an hour ago and is now fixed.

I don't know how to test it but my feeling is it won't help with your duplicate key error since apparently the error object created by the mysql node does not include `error.code`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 June 2025 12:08 UTC](https://discourse.nodered.org/t/handling-errors-in-mysql/95973/9 "2025-06-11T12:08:53Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
