|
| 1 | +--- |
| 2 | +layout: docs-user-guide |
| 3 | +toc: toc-user-guide.html |
| 4 | +title: Handling errors |
| 5 | +slug: handling-errors |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +Node-RED provides two ways for a node to report an error. It can either just |
| 10 | +write a message to the log or it can notify the runtime of the error and cause |
| 11 | +a flow to be triggered. |
| 12 | + |
| 13 | +If the error is only written to the log, you will see the message in the Debug |
| 14 | +sidebar and log output, but you will not be able to create a flow to handle it. |
| 15 | +These are [uncatchable errors](#uncatchable-errors). |
| 16 | + |
| 17 | +If it does notify the runtime properly, then it is a [catchable error](#catchable-errors) |
| 18 | +that can be used to trigger an error-handling flow. |
| 19 | + |
| 20 | +There is a third sort of error that can cause the Node-RED runtime to shutdown. These |
| 21 | +[`uncaughtException` errors](#uncaughtexception-errors) cannot be handled in the flow and are caused by bugs |
| 22 | +in nodes. |
| 23 | + |
| 24 | +This guide describes each of these error types in more detail and shows what can |
| 25 | +be done to handle them. |
| 26 | + |
| 27 | + |
| 28 | +### Logging errors |
| 29 | + |
| 30 | +When a node logs an error, it will appear in the Debug sidebar. |
| 31 | + |
| 32 | +<div style="width: 314px" class="figure"> |
| 33 | + <img src="images/error_debug.png" alt="Error message in the Debug sidebar"> |
| 34 | + <p class="caption">Error message in the Debug sidebar</p> |
| 35 | +</div> |
| 36 | + |
| 37 | +This shows the error message, the data/time of the error, and the node that logged |
| 38 | +the error. As with other Debug messages, hovering over it will highlight the node |
| 39 | +in the workspace. If it isn't in the current view, then clicking on the node's |
| 40 | +name in the top corner will reveal it in the workspace. |
| 41 | + |
| 42 | + |
| 43 | +### Catchable errors |
| 44 | + |
| 45 | +If a node notifies the runtime of an error then the Catch node can be used to |
| 46 | +create a flow to handle it. |
| 47 | + |
| 48 | +<div style="width: 660px" class="figure"> |
| 49 | + <img src="images/error_catch.png" alt="Catch node"> |
| 50 | + <p class="caption">Catch node</p> |
| 51 | +</div> |
| 52 | + |
| 53 | +If an error is caught by a Catch node, it *will not* be logged to the Debug sidebar. |
| 54 | + |
| 55 | +The message sent by the Catch will be the message provided by the node reporting |
| 56 | +the error. This message will have an `error` property set that provides information |
| 57 | +about the error: |
| 58 | + |
| 59 | + |
| 60 | +``` |
| 61 | +{ |
| 62 | + "topic": ..., |
| 63 | + "payload": ..., |
| 64 | + "error": { |
| 65 | + "message": "An error", |
| 66 | + "source": { |
| 67 | + "id": "2e25823d.fa3f7e", |
| 68 | + "type": "function", |
| 69 | + "name": "My Function", |
| 70 | + "count": 1 |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +The properties of `msg.error` are: |
| 77 | + |
| 78 | + - `msg.error`: |
| 79 | + - `message` - the error message |
| 80 | + - `source` - information about the node logging the error: |
| 81 | + - `id` - the source node id |
| 82 | + - `type` - the type of the source node |
| 83 | + - `name` - the name, if set, of the source node |
| 84 | + - `count` - how many times *this* message has been thrown by *this* node. |
| 85 | + This property is used by the runtime to detect messages stuck in a loop - where |
| 86 | + they are passed back to the source node which then logs the error again, and so on. |
| 87 | + The runtime will allow a message to loop 9 times before logging another, |
| 88 | + uncatchable, error to break the loop. Deleting this property will disable the check. |
| 89 | + |
| 90 | +If the message already had a `msg.error` property when the node reported the error, |
| 91 | +that property will be moved to `msg._error`. |
| 92 | + |
| 93 | +By default, the Catch node is configured to be triggered by all nodes on the same |
| 94 | +tab in the editor, but it can also be configured to target specific nodes on the tab. |
| 95 | + |
| 96 | +If you have two Catch nodes on the same tab and they both target the same node, |
| 97 | +then they will both be triggered by any errors reported by that node. |
| 98 | + |
| 99 | +If a Catch node is configured to be triggered by all nodes, it can also be configured |
| 100 | +to only trigger on errors that have not already been caught by another Catch node. |
| 101 | +This allows you to create error handling flows that target specific nodes and also |
| 102 | +have an error handler that will catch "everything else". |
| 103 | + |
| 104 | + |
| 105 | +### Uncatchable errors |
| 106 | + |
| 107 | +These are the errors a node writes to the log without notifying the runtime properly. |
| 108 | +They cannot be handled using the Catch node. |
| 109 | + |
| 110 | +The node *might* provide alternative ways for handling the error. For example, by updating |
| 111 | +its status property (which can be monitored with the Status node). It may send a message |
| 112 | +as normal but with some additional property set to indicate the error. |
| 113 | + |
| 114 | +You may want to contact the node's author to see if it can be updated to log the error |
| 115 | +properly. |
| 116 | + |
| 117 | + |
| 118 | +### `uncaughtException` errors |
| 119 | + |
| 120 | +These are a particular type of node.js error that can occur when a Node fails to |
| 121 | +properly handle an internal error. They cause the entire Node-RED runtime to |
| 122 | +shutdown as that is the only safe thing to do. |
| 123 | + |
| 124 | +It may sound extreme, but here is what the [node.js documentation](https://nodejs.org/api/process.html#process_warning_using_uncaughtexception_correctly) says about it: |
| 125 | + |
| 126 | +> Attempting to resume normally after an uncaught exception can be similar to pulling out the power cord when upgrading a computer. Nine out of ten times, nothing happens. But the tenth time, the system becomes corrupted. |
| 127 | +
|
| 128 | + |
| 129 | +The typical cause will be that a node has kicked off an asynchronous task and |
| 130 | +that task has hit an error. A well-written node will have registered an error |
| 131 | +handler for that task, but if there isn't one, the error will go uncaught. |
| 132 | + |
| 133 | +If you encounter this type of error then you should attempt to identify which node |
| 134 | +caused the error and raise an issue against it. This is not always easy due to the asynchronous nature of the error. |
| 135 | + |
| 136 | +The stack trace provided in the Node-RED log will provide some clues as to the |
| 137 | +nature of the asynchronous task that hit the error, which in turn may help you |
| 138 | +to identify the node at fault. |
0 commit comments