Skip to content

Commit 7bb962b

Browse files
committed
fix: Cleanup printed errors to match error codes
1 parent 6f7ad9e commit 7bb962b

File tree

7 files changed

+110
-69
lines changed

7 files changed

+110
-69
lines changed

pkg/lib/traverse-get.sh

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ bash_object.traverse-get() {
55

66
if [ -n "${TRACE_BASH_OBJECT_TRAVERSE+x}" ]; then
77
stdtrace.log 0 ''
8-
stdtrace.log 0 "CALL: bash_object.traverse: $*"
8+
stdtrace.log 0 "CALL: bash_object.traverse-get: $*"
99
fi
1010

1111
local final_value_type="$1"
@@ -30,25 +30,18 @@ bash_object.traverse-get() {
3030

3131
bash_object.trace_loop
3232

33-
# If 'key' is not a member of object, error
33+
# If 'key' is not a member of object or index of array, error
3434
if [ -z "${current_object["$key"]+x}" ]; then
3535
echo "Error: Key '$key' is not in object '$current_object_name'"
3636
exit 1
37-
# If 'key' is a member of object, then we check to see if it's an object, array, or string
3837
else
39-
local key_value="${current_object["$key"]}"
40-
38+
# If 'key' is a member of an object, or index of array
4139
if [ -n "${TRACE_BASH_OBJECT_TRAVERSE+x}" ]; then
42-
stdtrace.log 1 "key: '$key'"
43-
stdtrace.log 1 "key_value: '$key_value'"
44-
stdtrace.log 1 "current_object_name: '$current_object_name'"
45-
stdtrace.log 1 "current_object=("
46-
for debug_key in "${!current_object[@]}"; do
47-
stdtrace.log 1 " [$debug_key]='${current_object["$debug_key"]}'"
48-
done
49-
stdtrace.log 1 ")"
40+
stdtrace.log 2 "BLOCK: OBJECT/ARRAY"
5041
fi
5142

43+
local key_value="${current_object["$key"]}"
44+
5245
# If the 'key_value' is a virtual object, it starts with the byte sequence
5346
# This means we will be setting either an object or an array
5447
if [ "${key_value::2}" = $'\x1C\x1D' ]; then
@@ -60,86 +53,90 @@ bash_object.traverse-get() {
6053

6154
local -n current_object="$current_object_name"
6255

63-
# If we are not on the last element of the query, then do nothing. We have
64-
# already set 'current_object_name' and 'current_object', so at the next loop
65-
# iteration, the just-"dereferenced" virtual object will be evaluated
6656
if ((i+1 < ${#REPLIES[@]})); then
57+
# Do nothing, we have already set 'current_object'
58+
# for the next iteration
6759
:
68-
# If we are the last element, then we actually perform the get operation. Set
69-
# REPLY (and go to next loop)
7060
elif ((i+1 == ${#REPLIES[@]})); then
61+
# We are last element of query, return the object
7162
if [ "$final_value_type" = object ]; then
7263
case "$vmd_dtype" in
7364
object)
7465
REPLY=("${current_object[@]}")
7566
;;
7667
array)
77-
printf '%s\n' "Error: 'A query for type 'object' was given, but an array was found"
78-
return 1
68+
bash_object.util.traverse_fail 'ERROR_VALUE_INCORRECT_TYPE' 'Queried for object, but found array'
69+
return
70+
;;
71+
*)
72+
bash_object.util.traverse_fail 'ERROR_INTERNAL_INVALID_VOBJ' "vmd_dtype: $vmd_dtype"
73+
return
7974
;;
8075
esac
8176
elif [ "$final_value_type" = array ]; then
8277
case "$vmd_dtype" in
8378
object)
84-
printf '%s\n' "Error: 'A query for type 'array' was given, but an object was found"
85-
return 1
79+
bash_object.util.traverse_fail 'ERROR_VALUE_INCORRECT_TYPE' 'Queried for array, but found object'
80+
return
8681
;;
8782
array)
88-
# TODO: Perf: Use 'REPLY=("${current_object[@]}")'?
8983
local key=
9084
for key in "${!current_object[@]}"; do
9185
REPLY["$key"]="${current_object["$key"]}"
9286
done
9387
;;
88+
*)
89+
bash_object.util.traverse_fail 'ERROR_INTERNAL_INVALID_VOBJ' "vmd_dtype: $vmd_dtype"
90+
return
91+
;;
9492
esac
9593
elif [ "$final_value_type" = string ]; then
9694
case "$vmd_dtype" in
9795
object)
98-
printf '%s\n' "Error: 'A query for type 'string' was given, but an object was found"
99-
return 1
96+
bash_object.util.traverse_fail 'ERROR_VALUE_INCORRECT_TYPE' 'Queried for string, but found object'
97+
return
10098
;;
10199
array)
102-
printf '%s\n' "Error: 'A query for type 'string' was given, but an array was found"
103-
return 1
100+
bash_object.util.traverse_fail 'ERROR_VALUE_INCORRECT_TYPE' 'Queried for string, but found array'
101+
return
104102
;;
103+
*)
104+
bash_object.util.traverse_fail 'ERROR_INTERNAL_INVALID_VOBJ' "vmd_dtype: $vmd_dtype"
105+
return
105106
esac
107+
else
108+
bash_object.util.traverse_fail 'ERROR_INTERNAL_INVALID_PARAM' "final_value_type: $final_value_type"
109+
return
106110
fi
107111
fi
112+
# Otherwise, 'key_value' is a string
108113
else
109-
# If we are getting a string
110-
111114
if [ -n "${TRACE_BASH_OBJECT_TRAVERSE+x}" ]; then
112115
stdtrace.log 2 "BLOCK: STRING"
113116
fi
114117

115-
# If we are less than the last element in the query, and the object member has a type
116-
# of 'string', throw an error. This means the user expected an object to have a key
117-
# with type 'object', but the type really is 'string'
118118
if ((i+1 < ${#REPLIES[@]})); then
119+
# TODO
120+
echo "mu" >&3
121+
exit 2
119122
:
120123
elif ((i+1 == ${#REPLIES[@]})); then
121124
local value="${current_object["$key"]}"
122125
if [ "$final_value_type" = object ]; then
123-
printf '%s\n' "Error: bash-object: A query for type 'object' was given, but a string was found"
124-
exit 1
126+
bash_object.util.traverse_fail 'ERROR_VALUE_INCORRECT_TYPE' 'Queried for object, but found string'
127+
return
125128
elif [ "$final_value_type" = array ]; then
126-
printf '%s\n' "Error: bash-object: A query for type 'array' was given, but a string was found"
127-
exit 1
129+
bash_object.util.traverse_fail 'ERROR_VALUE_INCORRECT_TYPE' 'Queried for array, but found string'
130+
return
128131
elif [ "$final_value_type" = string ]; then
129132
REPLY="$value"
130133
fi
131134
fi
132135
fi
133136

137+
bash_object.trace_current_object
134138
if [ -n "${TRACE_BASH_OBJECT_TRAVERSE+x}" ]; then
135-
stdtrace.log 1 "current_object_name: '$current_object_name'"
136-
stdtrace.log 1 "current_object=("
137-
for debug_key in "${!current_object[@]}"; do
138-
stdtrace.log 1 " [$debug_key]='${current_object["$debug_key"]}'"
139-
done
140-
stdtrace.log 1 ")"
141-
stdtrace.log 1 "final_value: '$final_value'"
142-
stdtrace.log 1 "END BLOCK 2"
139+
stdtrace.log 1 "END BLOCK"
143140
fi
144141
fi
145142
done

pkg/lib/traverse-set.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
bash_object.traverse-set() {
44
if [ -n "${TRACE_BASH_OBJECT_TRAVERSE+x}" ]; then
55
stdtrace.log 0 ''
6-
stdtrace.log 0 "CALL: bash_object.traverse: $*"
6+
stdtrace.log 0 "CALL: bash_object.traverse-set: $*"
77
fi
88

99
# TODO: errors if vmd_dtype, final_value_type is not one of the known ones

pkg/lib/util/trace.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@ bash_object.trace_loop() {
1414
stdtrace.log 0 ")"
1515
fi
1616
}
17+
18+
bash_object.trace_current_object() {
19+
if [ -n "${TRACE_BASH_OBJECT_TRAVERSE+x}" ]; then
20+
stdtrace.log 0 "key: '$key'"
21+
stdtrace.log 0 "current_object_name: '$current_object_name'"
22+
stdtrace.log 0 "current_object=("
23+
for debug_key in "${!current_object[@]}"; do
24+
stdtrace.log 0 " [$debug_key]='${current_object["$debug_key"]}'"
25+
done
26+
stdtrace.log 0 ")"
27+
fi
28+
}

pkg/lib/util/util.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,28 @@ declare -gA ERRORS_BASH_OBJECT=(
44
[ERROR_VALUE_NOT_FOUND]='Attempted to access either a member of an object or an index of an array, but the member or index does not exist'
55
[ERROR_VALUE_INCORRECT_TYPE]='Attempted to get or set a value, but somewhere a value with a different type was expected'
66
[ERROR_INTERNAL_INVALID_VOBJ]='Internal virtual object has incorrect metadata'
7+
[ERROR_INTERNAL_INVALID_PARAM]='Internal parameter has an incorrect value'
78
)
89

10+
bash_object.util.traverse_fail() {
11+
local error_key="$1"
12+
local error_context="$2"
13+
14+
if [ -z "$error_context" ]; then
15+
error_context='<empty>'
16+
fi
17+
18+
local error_message="${ERRORS_BASH_OBJECT["$error_key"]}"
19+
20+
local error_output=
21+
printf -v error_output 'Failed to perform object operation:
22+
-> code: %s
23+
-> message: %s
24+
-> context: %s' "$error_key" "$error_message" "$error_context"
25+
26+
printf '%s' "$error_output"
27+
return 2
28+
}
929
# TODO
1030
stdtrace.log() {
1131
local level="$1"

tests/get-array.bats

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,46 @@
22

33
load './util/init.sh'
44

5-
@test "errors if final type is 'string' when expecting type 'array' 1" {
5+
@test "ERROR_VALUE_INCORRECT_TYPE on get-array'ing string" {
66
declare -A OBJECT=([my_key]='string_value2')
77

88
run bash_object.traverse-get array OBJECT '.my_key'
99

1010
assert_failure
11-
assert_line -p "A query for type 'array' was given, but a string was found"
11+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
12+
assert_line -p 'Queried for array, but found string'
1213
}
1314

14-
@test "errors if final type is 'string' when expecting type 'array' 2" {
15+
@test "ERROR_VALUE_INCORRECT_TYPE on get-array'ing string in object" {
1516
declare -A SUB_OBJECT=([nested]='string_value')
1617
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=object;&SUB_OBJECT')
1718

1819
run bash_object.traverse-get array OBJECT '.my_key.nested'
1920

2021
assert_failure
21-
assert_line -p "A query for type 'array' was given, but a string was found"
22+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
23+
assert_line -p 'Queried for array, but found string'
2224
}
2325

24-
@test "errors if final type is 'object' when expecting type 'string' 1" {
26+
@test "ERROR_VALUE_INCORRECT_TYPE on get-array'ing object" {
2527
declare -A SUB_OBJECT=([omicron]='pi')
2628
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=object;&SUB_OBJECT')
2729

2830
run bash_object.traverse-get array OBJECT '.my_key'
2931

3032
assert_failure
31-
assert_line -p "A query for type 'array' was given, but an object was found"
33+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
34+
assert_line -p 'Queried for array, but found object'
3235
}
3336

34-
@test "errors if final type is 'object' when expecting type 'string' 2" {
37+
@test "ERROR_VALUE_INCORRECT_TYPE on get-array'ing object in object" {
3538
declare -A SUB_SUB_OBJECT=([omicron]='pi')
3639
declare -A SUB_OBJECT=([nested]=$'\x1C\x1Dtype=object;&SUB_SUB_OBJECT')
3740
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=object;&SUB_OBJECT')
3841

3942
run bash_object.traverse-get array OBJECT '.my_key.nested'
4043

4144
assert_failure
42-
assert_line -p "A query for type 'array' was given, but an object was found"
45+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
46+
assert_line -p 'Queried for array, but found object'
4347
}

tests/get-object.bats

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,48 @@
22

33
load './util/init.sh'
44

5-
@test "errors if final type is 'string' when expecting type 'object' 1" {
5+
@test "ERROR_VALUE_INCORRECT_TYPE on get-object'ing string" {
66
declare -A OBJECT=([my_key]='string_value2')
77

88
run bash_object.traverse-get object OBJECT '.my_key'
99

1010
assert_failure
11-
assert_line -p "A query for type 'object' was given, but a string was found"
11+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
12+
assert_line -p 'Queried for object, but found string'
1213
}
1314

14-
@test "errors if final type is 'string' when expecting type 'object' 2" {
15+
@test "ERROR_VALUE_INCORRECT_TYPE on get-object'ing string inside object" {
1516
declare -A SUB_OBJECT=([nested]='string_value')
1617
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=object;&SUB_OBJECT')
1718

1819
run bash_object.traverse-get object OBJECT '.my_key.nested'
1920

2021
assert_failure
21-
assert_line -p "A query for type 'object' was given, but a string was found"
22+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
23+
assert_line -p 'Queried for object, but found string'
2224
}
2325

24-
@test "errors if final type is 'array' when expecting type 'object' 1" {
26+
@test "ERROR_VALUE_INCORRECT_TYPE on get-object'ing array" {
2527
declare -a SUB_ARRAY=(omicron pi rho)
2628
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=array;&SUB_ARRAY')
2729

2830
run bash_object.traverse-get object OBJECT '.my_key'
2931

3032
assert_failure
31-
assert_line -p "A query for type 'object' was given, but an array was found"
33+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
34+
assert_line -p 'Queried for object, but found array'
3235
}
3336

34-
@test "errors if final type is 'array' when expecting type 'object' 2" {
37+
@test "ERROR_VALUE_INCORRECT_TYPE on get-object'ing array inside object" {
3538
declare -a SUB_SUB_ARRAY=(omicron pi rho)
3639
declare -A SUB_OBJECT=([nested]=$'\x1C\x1Dtype=array;&SUB_SUB_ARRAY')
3740
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=array;&SUB_OBJECT')
3841

3942
run bash_object.traverse-get object OBJECT '.my_key.nested'
4043

4144
assert_failure
42-
assert_line -p "A query for type 'object' was given, but an array was found"
45+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
46+
assert_line -p 'Queried for object, but found array'
4347
}
4448

4549
# # { "stars": { "cool": "Wolf 359" } }

tests/get-string.bats

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,50 @@
22

33
load './util/init.sh'
44

5-
@test "errors if final type is 'object' when expecting type 'string' 1" {
5+
@test "ERROR_VALUE_INCORRECT_TYPE on get-string'ing object" {
66
declare -A SUB_OBJECT=([omicron]='pi')
77
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=object;&SUB_OBJECT')
88

99
run bash_object.traverse-get string OBJECT '.my_key'
1010

1111
assert_failure
12-
assert_line -p "A query for type 'string' was given, but an object was found"
12+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
13+
assert_line -p 'Queried for string, but found object'
1314
}
1415

15-
@test "errors if final type is 'object' when expecting type 'string' 2" {
16+
@test "ERROR_VALUE_INCORRECT_TYPE on get-string'ing object in object" {
1617
declare -A SUB_SUB_OBJECT=([omicron]='pi')
1718
declare -A SUB_OBJECT=([nested]=$'\x1C\x1Dtype=object;&SUB_SUB_OBJECT')
1819
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=object;&SUB_OBJECT')
1920

2021
run bash_object.traverse-get string OBJECT '.my_key.nested'
2122

2223
assert_failure
23-
assert_line -p "A query for type 'string' was given, but an object was found"
24+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
25+
assert_line -p 'Queried for string, but found object'
2426
}
2527

26-
@test "errors if final type is 'array' when expecting type 'string' 1" {
28+
@test "ERROR_VALUE_INCORRECT_TYPE on get-string'ing array" {
2729
declare -a SUB_ARRAY=(omicron pi rho)
2830
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=array;&SUB_ARRAY')
2931

3032
run bash_object.traverse-get string OBJECT '.my_key'
3133

3234
assert_failure
33-
assert_line -p "A query for type 'string' was given, but an array was found"
35+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
36+
assert_line -p 'Queried for string, but found array'
3437
}
3538

36-
@test "errors if final type is 'array' when expecting type 'string' 2" {
39+
@test "ERROR_VALUE_INCORRECT_TYPE on get-string'ing array in object" {
3740
declare -a SUB_SUB_ARRAY=(omicron pi rho)
3841
declare -A SUB_OBJECT=([nested]=$'\x1C\x1Dtype=array;&SUB_SUB_ARRAY')
3942
declare -A OBJECT=([my_key]=$'\x1C\x1Dtype=array;&SUB_OBJECT')
4043

4144
run bash_object.traverse-get string OBJECT '.my_key.nested'
4245

4346
assert_failure
44-
assert_line -p "A query for type 'string' was given, but an array was found"
47+
assert_line -p "ERROR_VALUE_INCORRECT_TYPE"
48+
assert_line -p 'Queried for string, but found array'
4549
}
4650

4751
@test "properly gets string in root" {

0 commit comments

Comments
 (0)