-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbashful-error.inc.sh
139 lines (116 loc) · 3.97 KB
/
bashful-error.inc.sh
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# Bashful is copyright 2009-2016 Dejay Clayton, all rights reserved:
# https://github.com/dejayc/bashful
# Bashful is licensed under the 2-Clause BSD License:
# http://opensource.org/licenses/BSD-2-Clause
# Declare the module name and dependencies.
declare BASHFUL_MODULE='error'
declare BASHFUL_MODULE_DEPENDENCIES='bashful'
# Verify execution context and module dependencies, and register the module.
{
declare BASHFUL_MODULE_VAR="BASHFUL_LOADED_${BASHFUL_MODULE}"
[[ -z "${!BASHFUL_MODULE_VAR-}" ]] || return 0
# Ensure the module is sourced, not executed, generating an error
# otherwise.
[[ "${BASH_ARGV}" != '' ]] || {
echo "ERROR: ${BASH_SOURCE[0]##*/} must be sourced, not executed"
exit 1
} >&2
# Register the module and dependencies.
declare "${BASHFUL_MODULE_VAR}"="${BASHFUL_MODULE}"
declare "BASHFUL_DEPS_${BASHFUL_MODULE}"="${BASHFUL_MODULE_DEPENDENCIES}"
}
# Bashful error code ranges:
# 0: Success
# 1: General script error
# 2: Usage error or help; or error executing built-in command
# 3 - 19: Error codes returned from utilities and shell
# 20 - 39: A user-supplied parameter was missing
# 40 - 59: A user-supplied parameter was invalid
# 60 - 79: A script-supplied parameter was missing
# 80 - 99: A script-supplied parameter was invalid
# 100 - 119: A resource specified by a parameter was inaccessible
# 120 - 139: Reserved for special shell exit codes
# 126: A command was not executable, due to permission or file issues
# 140 - 159: A configuration setting was missing
# 160 - 179: A configuration setting was invalid
# 180 - 199: A resource specified by a configuration setting was inaccessible
# 200 - 219: An internal script error occurred
# Default bash error codes:
# (please refer to http://www.tldp.org/LDP/abs/html/exitcodes.html)
# 0: Success
# 1: General script error
# 2: Usage error or help; or error executing built-in command
# 126: A command was not executable, due to permission or file issues
# 127: An illegal command was specified
# 128: Exit status was out of range
# 128 - 255: An error signal of (128 + n) was encountered. E.g. kill -9 = 137
# 130: Execution was terminated via Ctrl-C (128 + 2)
# 255: Exit status was out of range
function ERROR_commandExecution()
{
declare -i STATUS=${?}
declare -i ERR_CODE=3
if [ -n "${2-}" ]
then
let ERR_CODE="${2}"
else
[[ ${STATUS} -ne 0 ]] && let ERR_CODE=STATUS
fi
local COMMAND="${1?'INTERNAL ERROR: Command not specified'}"
stderr ${ERR_CODE} <<:ERROR
ERROR: A command encountered an error during execution
COMMAND: ${COMMAND}
ERROR CODE: ${ERR_CODE}
:ERROR
}
function ERROR_commandNotExecutable()
{
declare -i STATUS=${?}
declare -i ERR_CODE=126
if [ -n "${2-}" ]
then
let ERR_CODE="${2}"
else
[[ ${STATUS} -ne 0 ]] && let ERR_CODE=STATUS
fi
local COMMAND="${1?'INTERNAL ERROR: Command not specified'}"
stderr ${ERR_CODE} <<:ERROR
ERROR: A required command was not executable
COMMAND: ${COMMAND}
:ERROR
}
function ERROR_invalidSettingValue()
{
declare -i STATUS=${?}
declare -i ERR_CODE=160
if [ -n "${3-}" ]
then
let ERR_CODE="${3}"
else
[[ ${STATUS} -ne 0 ]] && let ERR_CODE=STATUS
fi
local SETTING_NAME="${1?'INTERNAL ERROR: Setting not specified'}"
local SETTING_VALUE="${2-}"
stderr ${ERR_CODE} <<:ERROR
ERROR: An invalid value was specified for a required script setting
SETTING: ${SETTING_NAME}
VALUE: ${SETTING_VALUE}
:ERROR
}
function ERROR_missingSetting()
{
declare -i STATUS=${?}
declare -i ERR_CODE=140
if [ -n "${2-}" ]
then
let ERR_CODE="${2}"
else
[[ ${STATUS} -ne 0 ]] && let ERR_CODE=STATUS
fi
local SETTING_NAME="${1?'INTERNAL ERROR: Setting not specified'}"
stderr ${ERR_CODE} <<:ERROR
ERROR: A required script setting was missing
SETTING: ${SETTING_NAME}
:ERROR
}