diff options
Diffstat (limited to 'lib/adei/adei_error.sh')
-rw-r--r-- | lib/adei/adei_error.sh | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/lib/adei/adei_error.sh b/lib/adei/adei_error.sh new file mode 100644 index 0000000..9de4795 --- /dev/null +++ b/lib/adei/adei_error.sh @@ -0,0 +1,74 @@ +# Error codes: 1 - unknown network, 2 - unreachable, 3 - timeout, 4 - invalid XML/JSON, 5 - parsing error, 6 - set is empty, 7 - service reported error, 8 - parameter error, 9 - unknown, 10-59 - HTTP errors +# http code (xyz) is converted to error code (xz), e.g. 404 -> 44 + +function adei_format_http_error { + err=$1 + echo -n "$(($err / 10))0$(($err % 10))" +} + +# Returns 1 if printing text is recommended +function adei_print_error { + local ret=0 + local code=${1:-0} + local text="$2" + + local service="${3#\*}" + service="${service:-ADEI}" + + local msg + if [ $code -ge 30 -a $code -lt 60 ]; then + msg="HTTP Error: $(adei_format_http_error $code)" + [ $code -eq 41 ] && msg+=" (Authentication Requested)" + [ $code -eq 43 ] && msg+=" (Access Denied)" + elif [ $code -ge 20 -a $code -lt 30 ]; then + msg="HTTP Status: $(adei_format_http_error $code)" + ret=1 + elif [ $code -eq 1 ]; then + msg="Error communicating with $service" + ret=1 + elif [ $code -eq 2 ]; then + msg="ADEI host ($adei_host:$adei_port) unreachable" + elif [ $code -eq 3 ]; then + msg="Timeout waiting for $service" + elif [ $code -eq 4 ]; then + msg="Non-XML response from $service" + ret=1 + elif [ $code -eq 5 ]; then + msg="Error parsing response from $service" + ret=1 + elif [ $code -eq 6 ]; then + msg="Empty response from $service" + ret=1 + elif [ $code -eq 7 ]; then + rep="$(echo "$text" | head -n 1 | sed 's/Error:\?[[:space:]]\+//i')" + if [ ${#rep} -lt 40 ]; then + msg="$service Error: $rep" + else + msg="$service returned a error" + ret=1 + fi + elif [ $code -eq 8 ]; then + msg="Invalid parameter" + else + msg="Unknown Error $code while accessing $service" + fi + + echo -n "$msg" + return $ret +} + +function adei_process_error { + local code=${1:-0}; shift + local text="$1"; shift + local service="$1"; shift + local status="${1:-0}" + + if [ $code -ne 0 ]; then + local msg # local on the same line break error reporting + msg="$(adei_print_error $code "$text" "$service")" + [ $? -eq 1 ] && echo "$text" + echo "$status $msg" + exit + fi +} + |