Closed vurtic closed 8 years ago
Haven't checked what error codes does python return, but the pause can be conditional to avoid stopping bat if everything worked fine. If it uses positive, greater than 0 values as errors:
python kanan.py
if errorlevel 1 goto dopause
exit
:dopause
echo There was a problem executing script. Check your python installation.
REM some extra message can be added here
pause
For negative values:
python kanan.py
if errorlevel 0 goto doexit
echo There was a problem executing script. Check your python installation.
REM some extra message can be added here
pause
:doexit
if errorlevel 0
is same as if last_return_code >= 0
While I am familiar with errorlevels uses it can also potentially cause more problems if it does not clear an assigned errorlevel. Interrupting the batch midway any time there is a fail or error seems fairly redundant and excessive in the code. A simple output file may be better for logging.
After some thought I don't believe errorlevels are a solution. See above.
Well, it's just a method of detecting if ruby did a clean exit and in case it didn't, pause execution to let user check the console output instead of closing the window automatically. It would run the exit
only if ruby returned without error. But if you're worried about that exit
, it can be as well replaced with a jump after pause, so the bat will continue executing whatever you'll put at the end of file in the future.
python kanan.py
if errorlevel 1 goto dopause
goto doresume
:dopause
echo ...
pause
:doresume
Unfortunately bat doesn't have any "cleaner" way of handling conditional code.
Actually, I was wrong about this one. Apparently this works:
IF errorlevel 1 (
echo Check your python installation.
pause
) ELSE (
echo No error.
)
btw, the return code is set either way whenever program exits, whether you use errorlevel or not, so there's nothing to clear, to be honest. Errorlevel is just a method to access it.
Of course, wrapping code in a try/catch and in case of unhandled exception writing log to a file is always welcome, but it may not detect all the problems.
Did you read somewhere in the documentation that Python always returns 1 on error?
Like I wrote in my first reply, haven't really checked the return codes for python, but gave 2 versions, depending if errors are indicated by positive or negative values. This can be adapted to actual error codes.
Also, errorlevel 1
checks if the return code is greater or equal to 1, not if it's just equal.
This could get handy for checking if python is even installed, since apparently errorlevel is set to 9009 in case that program is missing (source).
Does my commit satisfy the issue? Let me know if it doesn't and I'll reopen this issue.
Adding "pause" to the end of the .bat files will help users identify errors more easily.