Compare commits

...

5 Commits
v1.1.0 ... main

Author SHA1 Message Date
be21d3da70 Update go.mod 2024-12-16 19:45:51 +01:00
milarin
52aa31472f v2 implemented 2023-07-27 19:23:47 +02:00
milarin
1bc43397d6 renamed CallTrace to CallStack and implemented Error.Stack() method 2023-07-27 19:20:02 +02:00
milarin
ad6d42b35c added Error.Message method 2023-07-27 17:12:33 +02:00
milarin
e274a0473a Mustify functions implemented 2023-03-25 09:46:32 +01:00
5 changed files with 123 additions and 14 deletions

View File

@ -6,15 +6,15 @@ import (
"strings"
)
// CallTrace represents a call stack trace similar to Java's stack trace
type CallTrace struct {
// CallStack represents a call stack trace similar to Java's stack trace
type CallStack struct {
frames *runtime.Frames
more bool
}
// Trace returns a new CallTrace starting from this call
// Use skip to skip the first entries in the trace
func Trace(skip int) *CallTrace {
func Trace(skip int) *CallStack {
if DisableTrace {
return nil
}
@ -22,10 +22,10 @@ func Trace(skip int) *CallTrace {
pc := make([]uintptr, CallStackLength+1)
n := runtime.Callers(skip+1, pc)
pc = pc[:n]
return &CallTrace{runtime.CallersFrames(pc), n == CallStackLength+1}
return &CallStack{runtime.CallersFrames(pc), n == CallStackLength+1}
}
func (ct *CallTrace) String() string {
func (ct *CallStack) String() string {
if ct == nil {
return ""
}

View File

@ -9,7 +9,7 @@ import (
// Error is a wrapper for error with stack trace
type Error struct {
msg string
callTrace *CallTrace
callStack *CallStack
tmpl *ErrTmpl
cause error
prev []error
@ -19,7 +19,7 @@ type Error struct {
func New(msg string) *Error {
return &Error{
msg: msg,
callTrace: Trace(2),
callStack: Trace(2),
}
}
@ -28,7 +28,7 @@ func Wrap(msg string, cause error) *Error {
return &Error{
msg: msg,
cause: cause,
callTrace: Trace(2),
callStack: Trace(2),
}
}
@ -37,7 +37,7 @@ func Wrap(msg string, cause error) *Error {
func Chain(msg string, errors []error) *Error {
return &Error{
msg: msg,
callTrace: Trace(2),
callStack: Trace(2),
prev: errors,
}
}
@ -66,6 +66,14 @@ func (e *Error) Error() string {
return b.String()
}
func (e *Error) Message() string {
return e.msg
}
func (e *Error) Stack() *CallStack {
return e.callStack
}
// Is implements the error equality function used by errors.Is()
// It returns true if the error is the same instance or is created using the same ErrTmpl
func (e *Error) Is(target error) bool {
@ -133,7 +141,7 @@ func printErr(err error, b *strings.Builder) {
b.WriteString(": ")
b.WriteString(e.msg)
b.WriteString("\n")
b.WriteString(e.callTrace.String())
b.WriteString(e.callStack.String())
} else {
b.WriteString(errtype(err))
b.WriteString(": ")

View File

@ -30,7 +30,7 @@ func (t *ErrTmpl) New(args ...interface{}) *Error {
msg: fmt.Sprintf(t.format, args...),
cause: nil,
tmpl: t,
callTrace: Trace(2),
callStack: Trace(2),
}
}
@ -40,7 +40,7 @@ func (t *ErrTmpl) Wrap(cause error, args ...interface{}) *Error {
msg: fmt.Sprintf(t.format, args...),
cause: cause,
tmpl: t,
callTrace: Trace(2),
callStack: Trace(2),
}
}
@ -49,7 +49,7 @@ func (t *ErrTmpl) Wrap(cause error, args ...interface{}) *Error {
func (t *ErrTmpl) Chain(msg string, errors []error) *Error {
return &Error{
msg: msg,
callTrace: Trace(2),
callStack: Trace(2),
tmpl: t,
prev: errors,
}

2
go.mod
View File

@ -1,3 +1,3 @@
module git.milar.in/milarin/adverr
module git.tordarus.net/tordarus/adverr/v2
go 1.18

101
mustify.go Normal file
View File

@ -0,0 +1,101 @@
package adverr
// Mustify wraps the given function in a Must call
func Mustify[T any](f func() (value T, err error)) func() T {
return func() T { return Must(f()) }
}
// Mustify2 wraps the given function in a Must2 call
func Mustify2[A, B any](f func() (a A, b B, err error)) func() (A, B) {
return func() (A, B) { return Must2(f()) }
}
// Mustify3 wraps the given function in a Must3 call
func Mustify3[A, B, C any](f func() (a A, b B, c C, err error)) func() (A, B, C) {
return func() (A, B, C) { return Must3(f()) }
}
// Mustify4 wraps the given function in a Must4 call
func Mustify4[A, B, C, D any](f func() (a A, b B, c C, d D, err error)) func() (A, B, C, D) {
return func() (A, B, C, D) { return Must4(f()) }
}
// Mustify1i wraps the given function in a Must call
func Mustify1i[I, O any](f func(I) (O, error)) func(I) O {
return func(a I) O { return Must(f(a)) }
}
// Mustify1i2 wraps the given function in a Must2 call
func Mustify1i2[I, O1, O2 any](f func(I) (a O1, b O2, err error)) func(I) (O1, O2) {
return func(a I) (O1, O2) { return Must2(f(a)) }
}
// Mustify1i3 wraps the given function in a Must3 call
func Mustify1i3[I, O1, O2, O3 any](f func(I) (a O1, b O2, c O3, err error)) func(I) (O1, O2, O3) {
return func(a I) (O1, O2, O3) { return Must3(f(a)) }
}
// Mustify1i4 wraps the given function in a Must4 call
func Mustify1i4[I, O1, O2, O3, O4 any](f func(I) (a O1, b O2, c O3, d O4, err error)) func(I) (O1, O2, O3, O4) {
return func(a I) (O1, O2, O3, O4) { return Must4(f(a)) }
}
// Mustify2i wraps the given function in a Must call
func Mustify2i[I1, I2, O any](f func(I1, I2) (O, error)) func(I1, I2) O {
return func(a I1, b I2) O { return Must(f(a, b)) }
}
// Mustify2i2 wraps the given function in a Must2 call
func Mustify2i2[I1, I2, O1, O2 any](f func(I1, I2) (a O1, b O2, err error)) func(I1, I2) (O1, O2) {
return func(a I1, b I2) (O1, O2) { return Must2(f(a, b)) }
}
// Mustify2i3 wraps the given function in a Must3 call
func Mustify2i3[I1, I2, O1, O2, O3 any](f func(I1, I2) (a O1, b O2, c O3, err error)) func(I1, I2) (O1, O2, O3) {
return func(a I1, b I2) (O1, O2, O3) { return Must3(f(a, b)) }
}
// Mustify2i4 wraps the given function in a Must4 call
func Mustify2i4[I1, I2, O1, O2, O3, O4 any](f func(I1, I2) (a O1, b O2, c O3, d O4, err error)) func(I1, I2) (O1, O2, O3, O4) {
return func(a I1, b I2) (O1, O2, O3, O4) { return Must4(f(a, b)) }
}
// Mustify3i wraps the given function in a Must call
func Mustify3i[I1, I2, I3, O any](f func(I1, I2, I3) (O, error)) func(I1, I2, I3) O {
return func(a I1, b I2, c I3) O { return Must(f(a, b, c)) }
}
// Mustify3i2 wraps the given function in a Must2 call
func Mustify3i2[I1, I2, I3, O1, O2 any](f func(I1, I2, I3) (a O1, b O2, err error)) func(I1, I2, I3) (O1, O2) {
return func(a I1, b I2, c I3) (O1, O2) { return Must2(f(a, b, c)) }
}
// Mustify3i3 wraps the given function in a Must3 call
func Mustify3i3[I1, I2, I3, O1, O2, O3 any](f func(I1, I2, I3) (a O1, b O2, c O3, err error)) func(I1, I2, I3) (O1, O2, O3) {
return func(a I1, b I2, c I3) (O1, O2, O3) { return Must3(f(a, b, c)) }
}
// Mustify3i4 wraps the given function in a Must4 call
func Mustify3i4[I1, I2, I3, O1, O2, O3, O4 any](f func(I1, I2, I3) (a O1, b O2, c O3, d O4, err error)) func(I1, I2, I3) (O1, O2, O3, O4) {
return func(a I1, b I2, c I3) (O1, O2, O3, O4) { return Must4(f(a, b, c)) }
}
// Mustify4i wraps the given function in a Must call
func Mustify4i[I1, I2, I3, I4, O any](f func(I1, I2, I3, I4) (O, error)) func(I1, I2, I3, I4) O {
return func(a I1, b I2, c I3, d I4) O { return Must(f(a, b, c, d)) }
}
// Mustify4i2 wraps the given function in a Must2 call
func Mustify4i2[I1, I2, I3, I4, O1, O2 any](f func(I1, I2, I3, I4) (a O1, b O2, err error)) func(I1, I2, I3, I4) (O1, O2) {
return func(a I1, b I2, c I3, d I4) (O1, O2) { return Must2(f(a, b, c, d)) }
}
// Mustify4i3 wraps the given function in a Must3 call
func Mustify4i3[I1, I2, I3, I4, O1, O2, O3 any](f func(I1, I2, I3, I4) (a O1, b O2, c O3, err error)) func(I1, I2, I3, I4) (O1, O2, O3) {
return func(a I1, b I2, c I3, d I4) (O1, O2, O3) { return Must3(f(a, b, c, d)) }
}
// Mustify4i4 wraps the given function in a Must4 call
func Mustify4i4[I1, I2, I3, I4, O1, O2, O3, O4 any](f func(I1, I2, I3, I4) (a O1, b O2, c O3, d O4, err error)) func(I1, I2, I3, I4) (O1, O2, O3, O4) {
return func(a I1, b I2, c I3, d I4) (O1, O2, O3, O4) { return Must4(f(a, b, c, d)) }
}