summaryrefslogtreecommitdiffstats
path: root/lib/adei/adei_error.sh
blob: 63657fe673171a3565aeb6833111ec44885d041f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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:-80}) 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
}