Skip to content

Commit 03b7856

Browse files
committed
- Add stacktrace standard library
1 parent 340dd18 commit 03b7856

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/bashly/libraries/libraries.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ settings:
102102
- source: "settings/settings.yml"
103103
target: "settings.yml"
104104

105+
stacktrace:
106+
help: Add a function that shows stacktrace on error.
107+
files:
108+
- source: "stacktrace/stacktrace.sh"
109+
target: "%{user_lib_dir}/stacktrace.%{user_ext}"
110+
post_install_message: |
111+
The stacktrace function is designed to be called automatically on error.
112+
113+
To enable this functionality, add these lines to your `initialize.sh`:
114+
115+
g`trap 'stacktrace' ERR`
116+
g`set -o errtrace`
117+
105118
strings:
106119
help: Copy an additional configuration file to your project, allowing you to customize all the tips and error strings.
107120
files:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Stack trace functions [@bashly-upgrade stacktrace]
2+
## This file is a part of Bashly standard library
3+
##
4+
## Usage:
5+
## This function is designed to be called on error.
6+
##
7+
## To enable this functionality, add these lines to your `src/initialize.sh`
8+
## file (Run `bashly add hooks` to add this file):
9+
##
10+
## trap 'stacktrace' ERR
11+
## set -o errtrace
12+
##
13+
## Note that this functionality also requires `set -e`, which is enabled by
14+
## default in bashly generated scripts.
15+
##
16+
stacktrace() {
17+
local i=0
18+
local caller_output line func file
19+
20+
echo "${BASH_SOURCE[0]}:${BASH_LINENO[0]} in \`${FUNCNAME[1]}\`: $BASH_COMMAND"
21+
echo "Stack trace:"
22+
while caller_output="$(caller $i)"; do
23+
read -r line func file <<< "$caller_output"
24+
echo -e "\tfrom ${file}:${line} in \`$func\`"
25+
i=$((i + 1))
26+
done
27+
exit 1
28+
} >&2

0 commit comments

Comments
 (0)