Skip to content

Commit 5de3c66

Browse files
committed
refactor: Use core.print_error internally
This also improves the text of some error messages
1 parent 163eb6d commit 5de3c66

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ core.trap_remove 'some_handler' 'USR1'
6464

6565
Modifies current shell options and pushes information to stack, so
6666
it can later be easily undone. Note that it does not check to see if your Bash
67-
version supports the
67+
version supports the option
6868

6969
#### Example
7070

pkg/src/public/bash-core.sh

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ core.trap_add() {
2020

2121
# validation
2222
if [ -z "$function" ]; then
23-
printf '%s\n' "Error: core.trap_add: Function cannot be empty"
23+
core.print_error 'First argument must not be empty'
2424
return 1
2525
fi
2626

2727
if (($# <= 1)); then
28-
printf '%s\n' "Error: core.trap_add: Must specify at least one signal"
28+
core.print_error 'Must specify at least one signal'
2929
return 1
3030
fi
3131
for signal_spec in "${@:2}"; do
3232
if [ -z "$signal_spec" ]; then
33-
printf '%s\n' "Error: core.trap_add: Signal must not be an empty string"
33+
core.print_error 'Signal must not be an empty string'
3434
return 1
3535
fi
3636

3737
local regex='^[0-9]+$'
3838
if [[ "$signal_spec" =~ $regex ]]; then
39-
printf '%s\n' "Error: core.trap_add: Passing numbers for the signal specs is prohibited"
39+
core.print_error 'Passing numbers for the signal specs is prohibited'
4040
return 1
4141
fi; unset regex
4242
signal_spec=${signal_spec#SIG}
4343
if ! declare -f "$function" &>/dev/null; then
44-
printf '%s\n' "Error: core.trap_add: Function '$function' is not defined" >&2
44+
core.print_error "Function '$function' is not defined"
4545
return 1
4646
fi
4747

@@ -55,7 +55,7 @@ core.trap_add() {
5555
if ! eval "$global_trap_handler_name() {
5656
core.util.trap_handler_common '$signal_spec'
5757
}"; then
58-
printf '%s\n' "Error: core.trap_add: Could not eval function"
58+
core.print_error 'Could not eval function'
5959
return 1
6060
fi
6161
# shellcheck disable=SC2064
@@ -80,28 +80,28 @@ core.trap_remove() {
8080

8181
# validation
8282
if [ -z "$function" ]; then
83-
printf '%s\n' "Error: core.trap_remove: Function cannot be empty"
83+
core.print_error 'First argument must not be empty'
8484
return 1
8585
fi
8686

8787
if (($# <= 1)); then
88-
printf '%s\n' "Error: core.trap_remove: Must specify at least one signal"
88+
core.print_error 'Must specify at least one signal'
8989
return 1
9090
fi
9191
for signal_spec in "${@:2}"; do
9292
if [ -z "$signal_spec" ]; then
93-
printf '%s\n' "Error: core.trap_add: Signal must not be an empty string"
93+
core.print_error 'Signal must not be an empty string'
9494
return 1
9595
fi
9696

9797
local regex='^[0-9]+$'
9898
if [[ "$signal_spec" =~ $regex ]]; then
99-
printf '%s\n' "Error: core.trap_remove: Passing numbers for the signal specs is prohibited"
99+
core.print_error 'Passing numbers for the signal specs is prohibited'
100100
return 1
101101
fi; unset regex
102102
signal_spec="${signal_spec#SIG}"
103103
if ! declare -f "$function" &>/dev/null; then
104-
printf '%s\n' "Error: core.trap_remove: Function '$function' is not defined" >&2
104+
core.print_error "Function '$function' is not defined"
105105
return 1
106106
fi
107107

@@ -147,12 +147,12 @@ core.shopt_push() {
147147
local shopt_name="$2"
148148

149149
if [ -z "$shopt_action" ]; then
150-
printf '%s\n' "Error: core.shopt_push: First argument cannot be empty"
150+
core.print_error 'First argument cannot be empty'
151151
return 1
152152
fi
153153

154154
if [ -z "$shopt_name" ]; then
155-
printf '%s\n' "Error: core.shopt_push: Second argument cannot be empty"
155+
core.print_error 'Second argument cannot be empty'
156156
return 1
157157
fi
158158

@@ -174,7 +174,7 @@ core.shopt_push() {
174174
return $?
175175
fi
176176
else
177-
printf '%s\n' "Error: core.shopt_push: Accepted actions are either '-s' or '-u'" >&2
177+
core.print_error "Accepted actions are either '-s' or '-u'"
178178
return 1
179179
fi
180180

@@ -197,12 +197,12 @@ core.shopt_pop() {
197197
fi
198198

199199
if (( ${#___global_shopt_stack___[@]} == 0 )); then
200-
printf '%s\n' "Error: core.shopt_pop: Unable to pop as nothing is in the shopt stack"
200+
core.print_error 'Unable to pop as nothing is in the shopt stack'
201201
return 1
202202
fi
203203

204204
if (( ${#___global_shopt_stack___[@]} & 1 )); then
205-
printf '%s\n' "Fatal: core.shopt_pop: Shopt stack is malformed"
205+
core.print_error 'Shopt stack is malformed'
206206
return 1
207207
fi
208208

@@ -212,7 +212,7 @@ core.shopt_pop() {
212212

213213
if shopt -u "$shopt_name"; then :; else
214214
local errcode=$?
215-
printf '%s\n' "Fatal: core.shopt_pop: Could not restore previous option" >&2
215+
core.print_error 'Could not restore previous shopt option'
216216
return $errcode
217217
fi
218218

@@ -232,12 +232,12 @@ core.err_set() {
232232
ERRCODE=$1
233233
ERR=$2
234234
else
235-
printf '%s\n' "Error: core.err_set: Incorrect function arguments"
235+
core.print_error 'Incorrect function arguments'
236236
return 1
237237
fi
238238

239239
if [ -z "$ERR" ]; then
240-
printf '%s\n' "Error: core.err_set: Argument for 'ERR' cannot be empty"
240+
core.print_error "Argument for 'ERR' cannot be empty"
241241
return 1
242242
fi
243243
}
@@ -299,7 +299,7 @@ core.print_stacktrace() {
299299
done; unset -v i
300300

301301
if [ "$cd_failed" = 'yes' ]; then
302-
printf '%s\n' "Error: core.stacktrace_print: A 'cd' failed, so the stacktrace may include relative paths"
302+
core.print_error "A 'cd' failed, so the stacktrace may include relative paths"
303303
fi
304304
} >&2
305305

@@ -371,7 +371,7 @@ core.get_package_info() {
371371
local toml_file="$basalt_package_dir/basalt.toml"
372372

373373
if [ ! -f "$toml_file" ]; then
374-
printf '%s\n' "Error: core.get_package_info: File '$toml_file' could not be found"
374+
core.print_error "File '$toml_file' could not be found"
375375
fi
376376

377377
local regex="^[ \t]*${key_name}[ \t]*=[ \t]*['\"](.*)['\"]"

tests/trap.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ load './util/init.sh'
1616
run core.trap_add '' 'USR1'
1717

1818
assert_failure
19-
assert_output -p "Function cannot be empty"
19+
assert_output -p "First argument must not be empty"
2020
}
2121

2222
@test "core.trap_add fails when signal is not supplied" {
@@ -78,7 +78,7 @@ load './util/init.sh'
7878
run core.trap_remove '' 'USR1'
7979

8080
assert_failure
81-
assert_output -p "Function cannot be empty"
81+
assert_output -p "First argument must not be empty"
8282
}
8383

8484
@test "core.trap_remove fails when signal is not supplied" {

0 commit comments

Comments
 (0)