Skip to content

Commit a80a8ae

Browse files
committed
fix:(bootstrap-header): Unexpected behaviors on UNCAUGHT EXCEPTIONS
1 parent 72b5fb3 commit a80a8ae

6 files changed

Lines changed: 79 additions & 69 deletions

File tree

Bashbox.meta

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
NAME="BashBox"
22
CODENAME="bashbox"
33
AUTHORS=("AXON <[email protected]>")
4-
VERSION="0.2.8"
4+
VERSION="0.2.9"
55
DEPENDENCIES=(
66
"std::0.1.5"
77
"argbash::0.1.0"
88
)
99
REPOSITORY="https://github.com/axonasif/bashbox"
10-
BASHBOX_COMPAT="0.2.8~"
10+
BASHBOX_COMPAT="0.2.9~"

bashbox

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
#!/usr/bin/env bash
2-
main@bashbox%9836 ()
2+
main@bashbox%7973 ()
33
{
4+
function process::self::exit ()
5+
{
6+
kill -USR1 "$___self_PID"
7+
};
48
function log::error ()
59
{
6-
local _lasttoken="$_";
7-
local _exception_line="${1}";
8-
local _retcode="${2}";
9-
local IFS='
10-
';
10+
local _retcode="${2:-$?}";
11+
local _exception_line="$1";
1112
local _source="${BB_ERR_SOURCE:-"${BASH_SOURCE[-1]}"}";
12-
echo -e "[!!!] \033[1;31merror\033[0m[$_retcode]: ${_source##*/}[$BASH_LINENO]: ${STACK_TRACE:-"$_exception_line"}" 1>&2;
13-
if test -v STACK_TRACE; then
13+
echo -e "[!!!] \033[1;31merror\033[0m[$_retcode]: ${_source}[$BASH_LINENO]: ${BB_ERR_MSG:-"$_exception_line"}" 1>&2;
14+
if test -v BB_ERR_MSG; then
1415
{
15-
echo -e "STACK TRACE: (TOKEN: $_lasttoken) $_exception_line";
16+
echo -e "STACK TRACE: (TOKEN: $_exception_line)" 1>&2;
1617
local -i _frame=0;
1718
local _treestack='|--';
1819
local _line _caller _source;
1920
while read -r _line _caller _source < <(caller "$_frame"); do
2021
{
21-
echo "$_treestack ${_caller} >> ${_source##*/}::${_line}";
22+
echo "$_treestack ${_caller} >> ${_source##*/}::${_line}" 1>&2;
2223
_frame+=1;
2324
_treestack+='--'
2425
};
@@ -27,41 +28,42 @@ main@bashbox%9836 ()
2728
fi;
2829
return "$_retcode"
2930
};
30-
\command \trap 'exit' USR1;
31-
\command \trap 'STACK_TRACE="UNCAUGHT EXCEPTION" log::error "$BASH_COMMAND" $? || { kill -USR1 "$___self_PID"; }' ERR;
32-
\command \unalias -a;
31+
\command \unalias -a || exit;
32+
trap 'exit' USR1;
33+
trap 'BB_ERR_MSG="UNCAUGHT EXCEPTION" log::error "$BASH_COMMAND" || process::self::exit' ERR;
3334
set -eEuT -o pipefail;
3435
shopt -s inherit_errexit expand_aliases;
3536
___self="$0";
3637
___self_PID="$$";
37-
___MAIN_FUNCNAME="main@bashbox%9836";
38+
___MAIN_FUNCNAME="main@bashbox%7973";
3839
___self_NAME="BashBox";
3940
___self_CODENAME="bashbox";
4041
___self_AUTHORS=("AXON <[email protected]>");
41-
___self_VERSION="0.2.8";
42+
___self_VERSION="0.2.9";
4243
___self_DEPENDENCIES=("std::0.1.5" "argbash::0.1.0");
4344
___self_REPOSITORY="https://github.com/axonasif/bashbox";
44-
___self_BASHBOX_COMPAT="0.2.8~";
45+
___self_BASHBOX_COMPAT="0.2.9~";
4546
function bb_bootstrap_header ()
4647
{
48+
function process::self::exit ()
49+
{
50+
kill -USR1 "$___self_PID"
51+
};
4752
function log::error ()
4853
{
49-
local _lasttoken="$_";
50-
local _exception_line="${1}";
51-
local _retcode="${2}";
52-
local IFS='
53-
';
54+
local _retcode="${2:-$?}";
55+
local _exception_line="$1";
5456
local _source="${BB_ERR_SOURCE:-"${BASH_SOURCE[-1]}"}";
55-
echo -e "[!!!] \033[1;31merror\033[0m[$_retcode]: ${_source##*/}[$BASH_LINENO]: ${STACK_TRACE:-"$_exception_line"}" 1>&2;
56-
if test -v STACK_TRACE; then
57+
echo -e "[!!!] \033[1;31merror\033[0m[$_retcode]: ${_source}[$BASH_LINENO]: ${BB_ERR_MSG:-"$_exception_line"}" 1>&2;
58+
if test -v BB_ERR_MSG; then
5759
{
58-
echo -e "STACK TRACE: (TOKEN: $_lasttoken) $_exception_line";
60+
echo -e "STACK TRACE: (TOKEN: $_exception_line)" 1>&2;
5961
local -i _frame=0;
6062
local _treestack='|--';
6163
local _line _caller _source;
6264
while read -r _line _caller _source < <(caller "$_frame"); do
6365
{
64-
echo "$_treestack ${_caller} >> ${_source##*/}::${_line}";
66+
echo "$_treestack ${_caller} >> ${_source##*/}::${_line}" 1>&2;
6567
_frame+=1;
6668
_treestack+='--'
6769
};
@@ -70,9 +72,9 @@ main@bashbox%9836 ()
7072
fi;
7173
return "$_retcode"
7274
};
73-
\command \trap 'exit' USR1;
74-
\command \trap 'STACK_TRACE="UNCAUGHT EXCEPTION" log::error "$BASH_COMMAND" $? || { kill -USR1 "$___self_PID"; }' ERR;
75-
\command \unalias -a;
75+
\command \unalias -a || exit;
76+
trap 'exit' USR1;
77+
trap 'BB_ERR_MSG="UNCAUGHT EXCEPTION" log::error "$BASH_COMMAND" || process::self::exit' ERR;
7678
set -eEuT -o pipefail;
7779
shopt -s inherit_errexit expand_aliases
7880
};
@@ -530,7 +532,7 @@ EOF
530532
_box_dir="$_repo_source";
531533
if test ! -e "$_box_dir"; then
532534
{
533-
log::error "$_box_dir does not exist" 1 || exit
535+
log::error "$_box_dir does not exist" 1 || process::self::exit
534536
};
535537
fi;
536538
_arg_force=off;
@@ -552,7 +554,7 @@ EOF
552554
else
553555
if string::matches "$_repo_source" "[a-zA-Z0-9_]"; then
554556
{
555-
_repo_url="$(grep ".*/${_repo_source}$" "$_registry_meta_file")" || log::error "No such box as $_repo_source was found in the registry" 1 || exit;
557+
_repo_url="$(grep ".*/${_repo_source}$" "$_registry_meta_file")" || log::error "No such box as $_repo_source was found in the registry" 1 || process::self::exit;
556558
_box_dir="$_bashbox_registrydir/${_repo_source}-${_tag_name}";
557559
_box_name="${_repo_source##*/}"
558560
};
@@ -617,7 +619,7 @@ EOF
617619
{
618620
log::info "Compiling $_box in release mode";
619621
( subcommand::build --release "$_box_dir" 2>&1 ) || {
620-
log::error "Errors were found while compiling $_box, operation failed" 1 || exit
622+
log::error "Errors were found while compiling $_box, operation failed" 1 || process::self::exit
621623
};
622624
source "$_box_dir/$_bashbox_meta_name";
623625
_built_executable="$_box_dir/target/release/$CODENAME";
@@ -944,7 +946,7 @@ ${YELLOW}${_self_name} ${_subcommand_argv} --release --release -- arg1 arg2 \"st
944946
fi;
945947
if test ! -e "${_parsed_input}.sh" && test ! -e "${_parsed_input}"; then
946948
{
947-
log::error "$_input is missing" 1 || exit
949+
log::error "$_input is missing" 1 || process::self::exit
948950
};
949951
fi;
950952
if grep '\*;$' <<< "$(awk '{$1=$1;print}' <<<"$_input")" > /dev/null; then
@@ -1550,6 +1552,7 @@ EOF
15501552
done;
15511553
log::info "Installing to $_target_install_dir";
15521554
local _target_full_path="$_target_install_dir/$___self_CODENAME";
1555+
rm -f "$_target_full_path";
15531556
echo '#!/usr/bin/env bash' > "$_target_full_path";
15541557
declare -f "${___MAIN_FUNCNAME}" >> "$_target_full_path";
15551558
echo "${___MAIN_FUNCNAME} \"\$@\";" >> "$_target_full_path";
@@ -1558,6 +1561,8 @@ EOF
15581561
};
15591562
function subcommand::clean ()
15601563
{
1564+
read -r bruh < <(false);
1565+
read;
15611566
function print_help ()
15621567
{
15631568
println::helpgen ${_self_name^^}-${_subcommand_argv^^} --short-desc "${SUBCOMMANDS_DESC[4]}" --usage "${_self_name} $_subcommand_argv <path>" --examples "### The basic way:
@@ -1849,4 +1854,4 @@ For bugreports: $___self_REPOSITORY"
18491854
};
18501855
main "$@"
18511856
}
1852-
main@bashbox%9836 "$@";
1857+
main@bashbox%7973 "$@";

src/header.sh

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
function bb_bootstrap_header() {
2+
function process::self::exit() {
3+
kill -USR1 "$___self_PID";
4+
}
25
function log::error() {
3-
local _lasttoken="$_";
4-
local _exception_line="${1}";
5-
local _retcode="${2}";
6-
local IFS=$' \t\n'
6+
local _retcode="${2:-$?}";
7+
local _exception_line="$1";
78
local _source="${BB_ERR_SOURCE:-"${BASH_SOURCE[-1]}"}";
89

9-
>&2 echo -e "[!!!] \033[1;31merror\033[0m[$_retcode]: ${_source##*/}[$BASH_LINENO]: ${STACK_TRACE:-"$_exception_line"}";
10-
11-
# Handle stacktrace
12-
if test -v STACK_TRACE; then {
13-
echo -e "STACK TRACE: (TOKEN: $_lasttoken) $_exception_line";
14-
# local -i startFrom="1";
15-
local -i _frame=0;
16-
# local _stack;
17-
local _treestack='|--';
18-
local _line _caller _source;
10+
>&2 echo -e "[!!!] \033[1;31merror\033[0m[$_retcode]: ${_source}[$BASH_LINENO]: ${BB_ERR_MSG:-"$_exception_line"}";
1911

20-
# while _stack="$(caller $_frame 2>&1)"; do {
21-
while read -r _line _caller _source < <(caller "$_frame"); do {
22-
# if (( $_frame + 1 >= $startFrom ))
23-
# then
24-
# local -a _trace=( $_stack );
25-
# echo "${_treestack} ${_trace[1]} @@ ${_trace[@]:2}::${_trace[0]}";
26-
# fi
27-
echo "$_treestack ${_caller} >> ${_source##*/}::${_line}";
28-
_frame+=1;
29-
_treestack+='--';
12+
if test -v BB_ERR_MSG; then {
13+
>&2 echo -e "STACK TRACE: (TOKEN: $_exception_line)";
14+
# local -i startFrom="1";
15+
local -i _frame=0;
16+
# local _stack;
17+
local _treestack='|--';
18+
local _line _caller _source;
3019

31-
} done
20+
# while _stack="$(caller $_frame 2>&1)"; do {
21+
while read -r _line _caller _source < <(caller "$_frame"); do {
22+
# if (( $_frame + 1 >= $startFrom ))
23+
# then
24+
# local -a _trace=( $_stack );
25+
# echo "${_treestack} ${_trace[1]} @@ ${_trace[@]:2}::${_trace[0]}";
26+
# fi
27+
>&2 echo "$_treestack ${_caller} >> ${_source##*/}::${_line}";
28+
_frame+=1;
29+
_treestack+='--';
30+
} done
3231

3332
} fi
3433

3534
return "$_retcode";
3635
}
37-
\command \trap 'exit' USR1; # A workaround to properly catch error status from process substitution.
38-
# Eg `while read -r _bruh; do echo bruh; done < <(false)`.
39-
\command \trap 'STACK_TRACE="UNCAUGHT EXCEPTION" log::error "$BASH_COMMAND" $? || { kill -USR1 "$___self_PID"; }' ERR;
4036

41-
\command \unalias -a; # To Make sure external aliases are not interfering.
37+
\command \unalias -a || exit; # To Make sure external aliases are not interfering.
38+
trap 'exit' USR1; # A workaround to properly catch error status from process substitution.
39+
# Eg `while read -r _bruh; do echo bruh; done < <(false)`.
40+
trap 'BB_ERR_MSG="UNCAUGHT EXCEPTION" log::error "$BASH_COMMAND" || process::self::exit' ERR;
41+
4242
# set -o pipefail; # To grab the last return code from a pipe.
4343
# set -o errexit; # To exit immadiately after trapping ERR.
4444
# set -o errtrace; # To detect ERR on some bash builtin commands.
@@ -47,5 +47,5 @@ function bb_bootstrap_header() {
4747
# shopt -s inherit_errexit; # To TRAP process substitution error codes in parent. (Didnt work as I had thought...)
4848
# shopt -s expand_aliases; # To enable alias bash-builtin usage without interactive mode.
4949
set -eEuT -o pipefail;
50-
shopt -s inherit_errexit expand_aliases;
50+
shopt -s inherit_errexit expand_aliases;
5151
}

src/subcommand/build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
function subcommand::build()
22
{
3+
34
print_help()
45
{
56
println::helpgen ${_self_name^^}-${_subcommand_argv^^} \
@@ -106,7 +107,7 @@ ${YELLOW}${_self_name} ${_subcommand_argv} --release --release -- arg1 arg2 \"st
106107
# echo "Parsed_input: $_parsed_input"; # DEBUG
107108
if test ! -e "${_parsed_input}.sh" && test ! -e "${_parsed_input}"; then {
108109
# echo "$PWD"
109-
log::error "$_input is missing" 1 || exit;
110+
log::error "$_input is missing" 1 || process::self::exit;
110111
} fi
111112

112113
# Handle wildcard symbol loading

src/subcommand/clean.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
function subcommand::clean()
22
{
33

4+
read -r bruh < <(false);
5+
6+
read;
7+
48
print_help()
59
{
610
println::helpgen ${_self_name^^}-${_subcommand_argv^^} \

src/subcommand/install.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function subcommand::install() {
7272
if string::matches "$_repo_source" '^file://.*'; then { # Local file path
7373
_box_dir="$_repo_source";
7474
if test ! -e "$_box_dir"; then {
75-
log::error "$_box_dir does not exist" 1 || exit;
75+
log::error "$_box_dir does not exist" 1 || process::self::exit;
7676
} fi
7777
_arg_force=off; # Ignore --force arg
7878
_repo_url=;
@@ -88,7 +88,7 @@ function subcommand::install() {
8888
_box_name="${_repo_name}";
8989
} elif string::matches "$_repo_source" "[a-zA-Z0-9_]"; then { # Short repo name for registered hooks
9090
_repo_url="$(grep ".*/${_repo_source}$" "$_registry_meta_file")" \
91-
|| log::error "No such box as $_repo_source was found in the registry" 1 || exit;
91+
|| log::error "No such box as $_repo_source was found in the registry" 1 || process::self::exit;
9292
_box_dir="$_bashbox_registrydir/${_repo_source}-${_tag_name}";
9393
_box_name="${_repo_source##*/}";
9494
} fi
@@ -186,7 +186,7 @@ function subcommand::install() {
186186
log::info "Compiling $_box in release mode";
187187
(subcommand::build --release "$_box_dir" 2>&1) \
188188
|| {
189-
log::error "Errors were found while compiling $_box, operation failed" 1 || exit;
189+
log::error "Errors were found while compiling $_box, operation failed" 1 || process::self::exit;
190190
};
191191

192192
source "$_box_dir/$_bashbox_meta_name";

0 commit comments

Comments
 (0)