helpers2.1: when using ynh_die, also return the error via YNH_STDRETURN such that it can be obtained from the python and displayed in the main error message, to increase the chance that people may read it and have something more useful than "An error happened in the script"

This commit is contained in:
Alexandre Aubin 2024-06-30 18:53:41 +02:00
parent a48bfa67de
commit f2b5f0f22c
3 changed files with 10 additions and 3 deletions

View file

@ -134,12 +134,12 @@ EOF
# Fake an install of those dependencies to see the errors
# The sed command here is, Print only from 'Reading state info' to the end.
[[ -n "$problematic_dependencies" ]] && _ynh_apt_install $problematic_dependencies --dry-run 2>&1 | sed --quiet '/Reading state info/,$p' | grep -v "fix-broken\|Reading state info" >&2
ynh_die "Unable to install dependencies"
ynh_die "Unable to install apt dependencies"
}
rm --recursive --force "$TMPDIR" # Remove the temp dir.
# check if the package is actually installed
_ynh_apt_package_is_installed "${app_ynh_deps}" || ynh_die "Unable to install dependencies"
_ynh_apt_package_is_installed "${app_ynh_deps}" || ynh_die "Unable to install apt dependencies"
# Specific tweak related to Postgresql
# -> trigger postgresql regenconf if we may have just installed postgresql

View file

@ -4,7 +4,11 @@
#
# usage: ynh_die "Some message"
ynh_die() {
echo "$1" 1>&2
if [[ -n "${1:-}" ]]
then
python3 -c 'import yaml, sys; print(yaml.dump({"error": sys.stdin.read()}))' <<< "${1:-}" >>"$YNH_STDRETURN"
echo "${1:-}" 1>&2
fi
exit 1
}

View file

@ -538,6 +538,9 @@ def hook_exec_with_script_debug_if_failure(*args, **kwargs):
failed = True if retcode != 0 else False
if failed:
error = error_message_if_script_failed
# check more specific error message added by ynh_die in $YNH_STDRETURN
if isinstance(retpayload, dict) and "error" in retpayload:
error += " : " + retpayload["error"].strip()
logger.error(error_message_if_failed(error))
failure_message_with_debug_instructions = operation_logger.error(error)
if Moulinette.interface.type != "api":