Skip to content

Commit fc6c2ae

Browse files
committed
fix: Rework error handling
This includes renaming the identifiers of various errors, exclusively using 'bash_toml.parse_fail', and better output of the parser during tests
1 parent 36b0b42 commit fc6c2ae

File tree

4 files changed

+49
-66
lines changed

4 files changed

+49
-66
lines changed

linter.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# shellcheck shell=bash
22

3-
# TODO: expand to parse_fail
4-
53
file='./pkg/lib/cmd/bash-toml.sh'
6-
grep -n -B0 -A1 -e 'bash_toml.die' "$file" \
4+
grep -n -B0 -A1 -e 'bash_toml.parse_fail' "$file" \
75
| grep -vP '(^[0-9]*:|^--$)' \
86
| awk '
97
BEGIN {

pkg/lib/commands/parse.sh

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ bash_toml.do_parse() {
1111

1212
while IFS= read -rn 1 char; do
1313
if bash_toml.is.newline "$char"; then
14-
PARSER_LINE_NUMBER+=1
1514
PARSER_COLUMN_NUMBER=0
15+
PARSER_LINE_NUMBER+=1
1616
else
1717
PARSER_COLUMN_NUMBER+=1
1818
fi
1919

20-
bash_toml.debug
20+
bash_toml.token_history_add
2121

2222
case "$mode" in
2323
# State in which parser starts, and before any given TOML construct
@@ -27,10 +27,10 @@ bash_toml.do_parse() {
2727
elif bash_toml.is.newline "$char"; then
2828
:
2929
elif bash_toml.is.table "$char"; then
30-
bash_toml.die "Tables are not supported"
30+
bash_toml.parse_fail 'NOT_IMPLEMENTED' "Tables are not supported"
3131
return 1
3232
elif bash_toml.is.double_quote "$char"; then
33-
bash_toml.die "Quoted keys are not supported"
33+
bash_toml.parse_fail 'NOT_IMPLEMENTED' "Quoted keys are not supported"
3434
return 1
3535
elif bash_toml.is.valid_bare_key_char "$char"; then
3636
bash_toml.init_key_string "$char"
@@ -46,21 +46,21 @@ bash_toml.do_parse() {
4646
:
4747
elif bash_toml.is.newline "$char"; then
4848
# TODO: not being fired?
49-
bash_toml.die "Key name found without value 2"
49+
bash_toml.parse_fail 'KEY_ABSENT' "Key name found without value 2"
5050
return 1
5151
elif bash_toml.is.double_quote "$char"; then
52-
bash_toml.die "Double quote values are not supported"
52+
bash_toml.parse_fail 'NOT_IMPLEMENTED' "Double quote values are not supported"
5353
return 1
5454
mode='MODE_DOUBLEQUOTE_DURING_VALUE'
5555
bash_toml.init_value_string
5656
elif bash_toml.is.single_quote "$char"; then
5757
mode='MODE_SINGLEQUOTE_DURING_VALUE'
5858
bash_toml.init_value_string
5959
elif bash_toml.is.empty "$char"; then
60-
bash_toml.parse_fail 'INCOMPLETE_VALUE_ANY'
60+
bash_toml.parse_fail 'VALUE_STRING_INVALID'
6161
return 1
6262
else
63-
bash_toml.die "Datetime, Boolean, Float, Integer, Array, Inline Table, etc. etc. Are not supported"
63+
bash_toml.parse_fail 'NOT_IMPLEMENTED' "Datetime, Boolean, Float, Integer, Array, Inline Table, etc. etc. Are not supported"
6464
return 1
6565
fi
6666
;;
@@ -70,12 +70,12 @@ bash_toml.do_parse() {
7070
elif bash_toml.is.equals_sign "$char"; then
7171
mode='MODE_ANY_BEFORE_VALUE'
7272
elif bash_toml.is.newline "$char"; then
73-
bash_toml.die "Key name found without value"
73+
bash_toml.parse_fail 'KEY_INVALID' "Key name found without value"
7474
return 1
7575
elif bash_toml.is.valid_bare_key_char "$char"; then
7676
bash_toml.append_key_string "$char"
7777
elif bash_toml.is.empty "$char"; then
78-
bash_toml.parse_fail 'INCOMPLETE_KEY'
78+
bash_toml.parse_fail 'KEY_INVALID'
7979
return 1
8080
else
8181
bash_toml.parse_fail 'UNEXPECTED_BRANCH'
@@ -86,10 +86,10 @@ bash_toml.do_parse() {
8686
if bash_toml.is.equals_sign "$char"; then
8787
mode="MODE_ANY_BEFORE_VALUE"
8888
elif bash_toml.is.empty "$char"; then
89-
bash_toml.die "No equals sign found. End of file reached"
89+
bash_toml.parse_fail 'UNEXPECTED_CHARACTER' "No equals sign found. End of file reached"
9090
return 1
9191
else
92-
bash_toml.parse_fail 'INVALID_KEY'
92+
bash_toml.parse_fail 'KEY_INVALID'
9393
return 1
9494
fi
9595
;;
@@ -103,7 +103,7 @@ bash_toml.do_parse() {
103103
if bash_toml.is.single_quote "$char"; then
104104
mode='MODE_ANY_AFTER_VALUE'
105105
elif bash_toml.is.newline "$char"; then
106-
bash_toml.die "Newlines are not valid in single quote"
106+
bash_toml.parse_fail 'VALUE_STRING_INVALID' "Newlines are not valid in single quote"
107107
return 1
108108
else
109109
bash_toml.append_value_string "$char"
@@ -117,7 +117,7 @@ bash_toml.do_parse() {
117117
elif bash_toml.is.empty "$char"; then
118118
mode='MODE_DEFAULT'
119119
else
120-
bash_toml.die "Newline expected"
120+
bash_toml.parse_fail 'UNEXPECTED_CHARACTER' "Newline expected"
121121
return 1
122122
fi
123123
;;
@@ -130,12 +130,12 @@ bash_toml.do_parse() {
130130
:
131131
;;
132132
MODE_ANY_BEFORE_VALUE)
133-
bash_toml.die "Key name found without value theta"
133+
bash_toml.parse_fail 'UNEXPECTED_BRANCH' "Key name found without value theta"
134134
return 1
135135
;;
136136
MODE_BAREKEY_DURING_KEY)
137137
# i.g. `keyName`
138-
bash_toml.die "Key name found without value"
138+
bash_toml.parse_fail 'UNEXPECTED_BRANCH' "Key name found without value"
139139
return 1
140140
;;
141141
MODE_EQUALS_BEFORE)

pkg/lib/util/util.sh

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,55 @@
11
# shellcheck shell=bash
22

3-
declare -gA errors=(
4-
[NOT_IMPLEMENTED]='The feature has not been implemented'
5-
[UNEXPECTED_BRANCH]='Unaccounted value'
6-
[INCOMPLETE_KEY]='The key could not completely be parsed'
7-
[INVALID_KEY]='The key is not valid'
8-
[INCOMPLETE_VALUE_ANY]='The key did not have a proper value'
3+
declare -gA BASH_TOML_ERRORS=(
4+
[NOT_IMPLEMENTED]='TOML feature has not been implemented'
5+
[UNEXPECTED_BRANCH]='This branch was not supposed to be activated. Please submit an issue'
6+
[KEY_ABSENT]='Key does not have a value'
7+
[UNEXPECTED_CHARACTER]='An unexpected character was encountered' # Generalization of any of the following errors
8+
[KEY_INVALID]='The key is not valid'
9+
[VALUE_INVALID]='The value could not be parsed'
10+
[VALUE_STRING_INVALID]='The value was not valid'
911
)
1012

11-
bash_toml.debug() {
12-
if [ -n "${DEBUG+x}" ]; then
13-
printf '%s\n' "$mode ($char) at $PARSER_LINE_NUMBER:$PARSER_COLUMN_NUMBER"
14-
fi
15-
}
13+
declare -a token_history=()
1614

17-
bash_toml.die() {
18-
bash_toml.debug >&3
19-
cat <<-EOF
15+
# @description Appends to token history for improved error insight
16+
bash_toml.token_history_add() {
17+
local str=
18+
printf -v str '%s\n' "$mode ($char) at $PARSER_LINE_NUMBER:$PARSER_COLUMN_NUMBER"
2019

21-
char: $char
22-
mode: $mode
23-
EOF
20+
token_history+=("$str")
2421

25-
if [ "$TOML_MANUAL_ERROR" = yes ]; then
26-
TOML_ERROR="$1"
27-
return 1
28-
else
29-
if [[ -n "${NO_COLOR+x}" || $TERM = dumb ]]; then
30-
printf '%s\n' "Fatal: $1"
22+
if [ -n "${DEBUG_BASH_TOML+x}" ]; then
23+
if [ -n "${BATS_RUN_TMPDIR+x}" ]; then
24+
printf '%s\n' "$str" >&3
3125
else
32-
printf '\033[0;31m%s\033[0m\n' "Fatal: $1"
26+
printf '%s\n' "$str"
3327
fi
34-
exit 1
3528
fi
3629
}
3730

38-
39-
4031
bash_toml.parse_fail() {
41-
bash_toml.debug >&3
42-
cat <<-EOF
43-
44-
char: $char
45-
mode: $mode
46-
EOF
47-
4832
local error_key="$1"
4933
local error_context="$2"
5034

51-
local error_message="${errors[$error_key]}"
35+
local error_message="${BASH_TOML_ERRORS["$error_key"]}"
36+
37+
local error_output=
38+
printf -v error_output 'Failed to parse toml:
39+
-> code: %s
40+
-> message: %s
41+
-> context: %s
42+
-> history:' "$error_key" "$error_message" "$error_context"
5243

53-
printf -v error_output 'Failed to parse toml:\n -> code: %s\n -> message: %s\n -> context: %s' "$error_key" "$error_message" "$error_context"
44+
for history_item in "${token_history[@]}"; do
45+
printf -v error_output '%s\n - %s\n' "$error_output" "$history_item"
46+
done
5447

5548
if [ "$TOML_MANUAL_ERROR" = yes ]; then
5649
TOML_ERROR="$error_output"
5750
return 1
5851
else
5952
printf '%s' "$error_output"
60-
# if [[ -n "${NO_COLOR+x}" || $TERM = dumb ]]; then
61-
# printf '%s\n' "Fatal: $1"
62-
# else
63-
# printf '\033[0;31m%s\033[0m\n' "Fatal: $1"
64-
# fi
6553
exit 1
6654
fi
67-
68-
69-
7055
}

tests/fail.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ load './util/init.sh'
88
EOF
99

1010
assert_failure
11-
assert_output -p 'INCOMPLETE_KEY'
11+
assert_output -p 'KEY_INVALID'
1212
}
1313

1414
@test "fails on incomplete key 2" {
@@ -17,7 +17,7 @@ load './util/init.sh'
1717
EOF
1818

1919
assert_failure
20-
assert_output -p 'INVALID_KEY'
20+
assert_output -p 'KEY_INVALID'
2121
}
2222

2323
@test "fails on incomplete key 3" {
@@ -26,5 +26,5 @@ load './util/init.sh'
2626
EOF
2727

2828
assert_failure
29-
assert_output -p 'INCOMPLETE_VALUE_ANY'
29+
assert_output -p 'VALUE_STRING_INVALID'
3030
}

0 commit comments

Comments
 (0)