Skip to content

Commit 06577bb

Browse files
committed
feat: Add fns for better error reporting
1 parent c5758de commit 06577bb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

docs/api.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ Core functions for any Bash program
1313
* [core.err_set()](#coreerr_set)
1414
* [core.err_clear()](#coreerr_clear)
1515
* [core.err_exists()](#coreerr_exists)
16+
* [core.err_print()](#coreerr_print)
1617
* [core.print_stacktrace()](#coreprint_stacktrace)
1718
* [core.print_error()](#coreprint_error)
1819
* [core.print_warn()](#coreprint_warn)
1920
* [core.print_info()](#coreprint_info)
21+
* [core.panic()](#corepanic)
2022
* [core.should_output_color()](#coreshould_output_color)
2123
* [core.get_package_info()](#coreget_package_info)
2224
* [core.init()](#coreinit)
@@ -126,6 +128,10 @@ _does_ exist
126128

127129
_Function has no arguments._
128130

131+
### core.err_print()
132+
133+
Prints the current error stored
134+
129135
### core.print_stacktrace()
130136

131137
Prints stacktrace
@@ -167,6 +173,10 @@ Print an informative message to standard output
167173

168174
* **$1** (string): message
169175

176+
### core.panic()
177+
178+
Use when a serious fault occurs. It will print the current ERR (if it exists)
179+
170180
### core.should_output_color()
171181

172182
Determine if color should be printed. Note that this doesn't

pkg/src/public/bash-core.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ core.err_exists() {
263263
fi
264264
}
265265

266+
# @description Prints the current error stored
267+
core.err_print() {
268+
printf '%s\n' 'Error found:'
269+
printf '%s\n' " ERRCODE: $ERRCODE" >&2
270+
printf '%s\n' " ERR: $ERR" >&2
271+
}
272+
266273
# @description Prints stacktrace
267274
# @noargs
268275
# @example
@@ -293,6 +300,7 @@ core.print_stacktrace() {
293300

294301
printf '%s\n' " in ${FUNCNAME[$i]} ($file:${BASH_LINENO[$i-1]})"
295302

303+
# shellcheck disable=SC1007
296304
if ! CDPATH= cd -- "$old_cd"; then
297305
cd_failed='yes'
298306
fi
@@ -327,6 +335,17 @@ core.print_info() {
327335
printf '%s\n' "Info: ${FUNCNAME[1]}${msg:+": "}$msg"
328336
}
329337

338+
# @description Use when a serious fault occurs. It will print the current ERR (if it exists)
339+
core.panic() {
340+
local code="${1-1}"
341+
342+
if core.err_exists; then
343+
core.err_print
344+
fi
345+
core.print_stacktrace
346+
exit "$code"
347+
}
348+
330349
# @description Determine if color should be printed. Note that this doesn't
331350
# use tput because simple environment variable checking heuristics suffice
332351
core.should_output_color() {

0 commit comments

Comments
 (0)