I have a shell script that I use on gitlab CI that runs a supplied command. The issue is it doesn't display errors if the command failed, it just says ERROR: Job failed: exit code 1. I tried the script locally and it was able to output the failed command, is there a way I could somehow force it to display the error through my script before it exits the job?
The particular part of my script
output="$( (cd "$FOLDER"; eval "$@") 2>&1 )"
if [ "$output" ]; then
echo -e "$output\n"
fi
One way to trap an error that works in every shell is to combine two commands with logical OR ||.
Catch error from subshell:
output="$( (cd "$FOLDER"; eval "$@") 2>&1 )" || errorcode="$?"
will save the error code from the previous command if it fails.
Exit with own error code if important command fails
output="$( (cd "$FOLDER"; eval "$@") 2>&1 )" || exit 12
For more complex things one can define a function that will be called after the OR.
handle_error() {
# do stuff
}
output="$( (cd "$FOLDER"; eval "$@") 2>&1 )" || handle_error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With