Jacques44 / node-red-contrib-bigssh

remote command using ssh big node
Apache License 2.0
8 stars 12 forks source link

How to handle / catch errors? #35

Open jerkstorecaller opened 2 years ago

jerkstorecaller commented 2 years ago

My scenario is this: I'd like to use bigssh to run a remote command that might print warnings to stderr, even for invocations that run fine (exit code 0). I also need to handle actual errors (exit code not 0) to detect real failures.

I'm not sure how I'm supposed to do this, without doing something fragile like text parsing stderr looking for the word "Return code" since that's bigssh's error message. The 3rd output doesn't give an exit code. And the error, despite stopping the flow, doesn't appear to be handlable in a Catch node.

Any advice?

Here's an example flow:

[
    {
        "id": "f9409b6cb34e08bc",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "368b47d307b01b06",
        "type": "bigssh",
        "z": "f9409b6cb34e08bc",
        "name": "query-remote-system",
        "commandLine": "badcmd",
        "commandArgs": "",
        "minError": 1,
        "minWarning": 1,
        "noStdin": false,
        "format": "utf8",
        "payloadIsArg": false,
        "myssh": "76f22fcf7a69e187",
        "x": 360,
        "y": 120,
        "wires": [
            [
                "673491caacc9f7f7"
            ],
            [],
            []
        ]
    },
    {
        "id": "3707e406739b9d18",
        "type": "inject",
        "z": "f9409b6cb34e08bc",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 140,
        "y": 120,
        "wires": [
            [
                "368b47d307b01b06"
            ]
        ]
    },
    {
        "id": "e531dce5ce945878",
        "type": "catch",
        "z": "f9409b6cb34e08bc",
        "name": "handle-errors",
        "scope": null,
        "uncaught": false,
        "x": 250,
        "y": 300,
        "wires": [
            [
                "1c540326e5abcc56"
            ]
        ]
    },
    {
        "id": "1c540326e5abcc56",
        "type": "debug",
        "z": "f9409b6cb34e08bc",
        "name": "all-bad",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 420,
        "y": 300,
        "wires": []
    },
    {
        "id": "673491caacc9f7f7",
        "type": "debug",
        "z": "f9409b6cb34e08bc",
        "name": "all-good",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 600,
        "y": 120,
        "wires": []
    },
    {
        "id": "76f22fcf7a69e187",
        "type": "SSH_Credentials",
        "host": "10.2.0.13",
        "port": "22",
        "userlabel": "user@10.2.0.13"
    }
]
Jacques44 commented 1 year ago

Hello. The node has 3 outputs as described in the main page. If your command send data to stdout (first output) or stderr (third output) you should be able to parse it within "bigline" and a function node. The exit code is available through the second output in the control property. Il you debug it and display the whole message, you'll see a lot of properties as rc, error text, speed etc.

thepapallot commented 4 months ago

Hello, I am quite new with nodered but I am using bigssh to manage some Raspberry pi's. Everything seems to work very good except when the raspberry pi is not connected by any reason. I don't know how to handle the error when ssh connection is not successful. I have tried the third output of bigssh and also the catch node without any luck.

I get the following error: 17/5/2024 13:34:24node: c2ed36b0e2af7350 msg : error "Error: connect ECONNREFUSED 192.168.1.69:22"

My example flow is the following:

#

[
    {
        "id": "97f510138f051eca",
        "type": "tab",
        "label": "Test",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "d3eed867e6516b27",
        "type": "inject",
        "z": "97f510138f051eca",
        "name": "",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 170,
        "y": 60,
        "wires": [
            [
                "c2ed36b0e2af7350"
            ]
        ]
    },
    {
        "id": "c2ed36b0e2af7350",
        "type": "bigssh",
        "z": "97f510138f051eca",
        "name": "",
        "commandLine": "echo 'test'",
        "commandArgs": "",
        "minError": 1,
        "minWarning": 1,
        "noStdin": false,
        "format": "ascii",
        "payloadIsArg": false,
        "myssh": "ad4aa2452678a8ae",
        "x": 510,
        "y": 200,
        "wires": [
            [
                "0a21c50a4c64dfd4",
                "81e8edc0a3f041d9"
            ],
            [],
            [
                "0e730c70a853846f"
            ]
        ]
    },
    {
        "id": "0a21c50a4c64dfd4",
        "type": "debug",
        "z": "97f510138f051eca",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 960,
        "y": 80,
        "wires": []
    },
    {
        "id": "81e8edc0a3f041d9",
        "type": "switch",
        "z": "97f510138f051eca",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "test\n",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 770,
        "y": 180,
        "wires": [
            [
                "c96857524b59b5dd"
            ]
        ]
    },
    {
        "id": "c96857524b59b5dd",
        "type": "debug",
        "z": "97f510138f051eca",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 910,
        "y": 140,
        "wires": []
    },
    {
        "id": "0e730c70a853846f",
        "type": "function",
        "z": "97f510138f051eca",
        "name": "Handle Error",
        "func": "if (msg.payload && msg.payload.includes('ECONNREFUSED')) {\n    msg.error = 'Connection refused to SSH server';\n    return msg;\n}",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 750,
        "y": 300,
        "wires": [
            [
                "1e0a42bc8d6d9b83"
            ]
        ]
    },
    {
        "id": "1e0a42bc8d6d9b83",
        "type": "debug",
        "z": "97f510138f051eca",
        "name": "Error",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "error",
        "targetType": "msg",
        "x": 910,
        "y": 300,
        "wires": []
    },
    {
        "id": "ad4aa2452678a8ae",
        "type": "SSH_Credentials",
        "host": "192.168.1.69",
        "port": "22",
        "userlabel": "pi@192.168.1.69"
    }
]