errs
simple try/catch error handling for Go
git clone https://git.pi66.xyz/errs
About | Files | Commits | Refs | License
fix: record throw call site and keep wrapper message on re-throw
Author: pi66
Date: 2026-10-27 17:07
commit: 48f3c516
Stat
| M | errs.go | 44 | +37 | -7 |
Diff
diff --git a/errs.go b/errs.go
index f282bf1..92d51ab 100644
--- a/errs.go
+++ b/errs.go
@@ -2,23 +2,53 @@ package errs
import (
"errors"
+ "runtime"
"runtime/debug"
)
type Error struct {
- Err error
- Stack []byte
+ Err error
+ Stack []byte
+ Function string
+ File string
+ Line int
...
Show all
Show less
diff --git a/errs.go b/errs.go
index f282bf1..92d51ab 100644
--- a/errs.go
+++ b/errs.go
@@ -2,23 +2,53 @@ package errs
import (
"errors"
+ "runtime"
"runtime/debug"
)
type Error struct {
- Err error
- Stack []byte
+ Err error
+ Stack []byte
+ Function string
+ File string
+ Line int
...
diff --git a/errs.go b/errs.go
index f282bf1..92d51ab 100644
--- a/errs.go
+++ b/errs.go
@@ -2,23 +2,53 @@ package errs
import (
"errors"
+ "runtime"
"runtime/debug"
)
type Error struct {
- Err error
- Stack []byte
+ Err error
+ Stack []byte
+ Function string
+ File string
+ Line int
}
func (e *Error) Error() string { return e.Err.Error() }
func (e *Error) Unwrap() error { return e.Err }
-func throw(e error) {
- var x *Error
- if errors.As(e, &x) {
- panic(&Error{e, x.Stack})
+func throw(err error) {
+ if err == nil {
+ return
}
- panic(&Error{e, debug.Stack()})
+ var existing *Error
+ if errors.As(err, &existing) {
+ if existing == err {
+ panic(existing)
+ }
+ panic(&Error{
+ Err: err,
+ Stack: existing.Stack,
+ Function: existing.Function,
+ File: existing.File,
+ Line: existing.Line,
+ })
+ }
+
+ e := &Error{
+ Err: err,
+ Stack: debug.Stack(),
+ }
+
+ pcs := make([]uintptr, 1)
+ if n := runtime.Callers(3, pcs); n > 0 {
+ frame, _ := runtime.CallersFrames(pcs).Next()
+ e.Function = frame.Function
+ e.File = frame.File
+ e.Line = frame.Line
+ }
+
+ panic(e)
}
func Catch(err *error, fn ...func(*error)) {