Get the error from paython file

Hi, I call to python(that make a call to s3) file and I throw in him an exception, I want to catch the message and to decide by the error code if I want to recall the node or terminate the workflow, any suggestion to a accomplish that

If you’re calling Python, I’d assume you are using the Execute Command node? This node gives you the exitCode as well as the stderr-output of your command:

You could check this using an IF node following your Execute Command node and then take action depending on the outcome. To terminate the workflow early, you can use a Stop And Error node.

1 Like

Thanks, how can I send a specific error code?

Send an exit code from your Python script you mean? That’d be sys.exit(yourErrorCodeHere).

1 Like

I try sys.exit(1) but I errorCode don’t change it’s always 0

Do you have an example workflow you can share? Preferably reduced to the relevant part, so a simple Execute Command node running a command resulting in an error (but still showing an exitCode of 0) would be great. Also, could you confirm the version of n8n you are running and how you have deployed n8n? Many thanks!

I just make the simplest file that returns sys.exit(1), if I run the same python file in my machine it work correctly

Hi @Asaf_Shay, this is most likely an error with your python script. Here’s a quick example where the error flow gets triggered:

Example Workflow

The Python Script is really simple:

import sys
print("bye")
sys.exit(1)

The error path of the flow will be executed since there is no exit code 0:

Changing sys.exit(1) to sys.exit(0) or sys.exit() causes the success path to be executed as expected:

I always get to the error path even if I change it to sys.exit(0)

What error are you getting? And did you enable the Continue On Fail setting on your Execute Command node as in the above example?

where is that option? Continue On Fail

It should already be enabled when copying the example I have provided into your canvas. You can also set this manually on the settings tab of your node:

yes it’s on, the error message I get
“Command failed: python3 -c ’ import sys print(“bye”) sys.exit(0) ’ Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.”

So that sounds like a pretty helpful error message coming from your OS there. It suggests Python (or the python3 command) is not available to your n8n instance which is why sys.exit won’t do anything. You’d need to install that first (same as with every other binary you want to execute through the Execute Command node).

I quickly tried typing python3 without anything else in my Windows shell as suggested by your error message and it opened the Microsoft Store on the right page where I could install Python 3.9 with a single click - maybe give this a go?

I try this

i try it also with python3 (after install) and the same problem, I think he have problem with the import

@Asaf_Shay I think n8n will try and run it as one line so the line breaks are probably messing it up. Have you tried running a python script instead?

1 Like

so how it works for MutedJam ? it’s the same code

Different systems use different line ending characters. The error coming back is saying it is getting an end of line (EOL) while working with a literal string.

If it was an import error you would get something back about the module not being found. You could probably run it as one line that would work.