Skip to content

Commit 43cbb6c

Browse files
committed
fix: Properly pass exit code through. Closes #4
The program for example now quits with exit code `4` if the user calls a function with `return 4` and `set -e` is enabled
1 parent 23a0aba commit 43cbb6c

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

docs/api.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Core functions for any Bash program
3030
### core.trap_add()
3131

3232
Adds a handler for a particular `trap` signal or event. Noticably,
33-
unlike the 'builtin' trap, this does not override any other existing handlers
33+
unlike the 'builtin' trap, this does not override any other existing handlers. The first argument
34+
to the handler is the exit code of the last command that ran before the particular 'trap'
3435

3536
#### Example
3637

@@ -143,9 +144,11 @@ Prints stacktrace
143144

144145
```bash
145146
err_handler() {
146-
local exit_code=$?
147+
local exit_code=$1 # Note that this isn't `$?`
147148
core.print_stacktrace
148-
exit $exit_code
149+
150+
# Note that we're not doing `exit $exit_code` because
151+
# that is handled automatically
149152
}
150153
core.trap_add 'err_handler' ERR
151154
```

pkg/src/public/bash-core.sh

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
# @description Core functions for any Bash program
55

66
# @description Adds a handler for a particular `trap` signal or event. Noticably,
7-
# unlike the 'builtin' trap, this does not override any other existing handlers
7+
# unlike the 'builtin' trap, this does not override any other existing handlers. The first argument
8+
# to the handler is the exit code of the last command that ran before the particular 'trap'
89
# @arg $1 string Function to execute on an event. Integers are forbiden
910
# @arg $2 string Event signal
1011
# @example
@@ -48,9 +49,16 @@ core.trap_add() {
4849
printf -v global_trap_handler_name '%q' "core.private.trap_handler_${signal_spec}"
4950

5051
if ! eval "$global_trap_handler_name() {
51-
core.private.util.trap_handler_common '$signal_spec'
52+
local ___exit_code_original=\$?
53+
if core.private.util.trap_handler_common '$signal_spec' \"\$___exit_code_original\"; then
54+
return \$___exit_code_original
55+
else
56+
local ___exit_code_user=\$?
57+
core.print_error_fn \"User-provided trap handler spectacularly failed with exit code \$___exit_code_user\"
58+
return \$___exit_code_user
59+
fi
5260
}"; then
53-
core.panic 'Could not eval function'
61+
core.panic 'Failed to eval function'
5462
fi
5563
# shellcheck disable=SC2064
5664
trap "$global_trap_handler_name" "$signal_spec"
@@ -265,9 +273,11 @@ core.panic() {
265273
# @noargs
266274
# @example
267275
# err_handler() {
268-
# local exit_code=$?
276+
# local exit_code=$1 # Note that this isn't `$?`
269277
# core.print_stacktrace
270-
# exit $exit_code
278+
#
279+
# # Note that we're not doing `exit $exit_code` because
280+
# # that is handled automatically
271281
# }
272282
# core.trap_add 'err_handler' ERR
273283
core.print_stacktrace() {

pkg/src/util/util.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ core.private.util.init() {
1414
# @internal
1515
core.private.util.trap_handler_common() {
1616
local signal_spec="$1"
17+
local code="$2"
1718

1819
local trap_handlers=
1920
IFS=$'\x1C' read -ra trap_handlers <<< "${___global_trap_table___[$signal_spec]}"
@@ -25,7 +26,9 @@ core.private.util.trap_handler_common() {
2526
fi
2627

2728
if declare -f "$trap_handler" &>/dev/null; then
28-
"$trap_handler"
29+
if "$trap_handler" "$code"; then :; else
30+
return $?
31+
fi
2932
else
3033
printf "%s\n" "Warn: core.trap_add: Function '$trap_handler' registered for signal '$signal_spec' no longer exists. Skipping" >&2
3134
fi

0 commit comments

Comments
 (0)