Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
be21d3da70 | |||
![]() |
52aa31472f | ||
![]() |
1bc43397d6 | ||
![]() |
ad6d42b35c | ||
![]() |
e274a0473a | ||
![]() |
9b95bdb3dd |
10
calltrace.go
10
calltrace.go
@ -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 ""
|
||||
}
|
||||
|
18
error.go
18
error.go
@ -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(": ")
|
||||
|
@ -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,
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@ -1,3 +1,3 @@
|
||||
module git.milar.in/milarin/adverr
|
||||
module git.tordarus.net/tordarus/adverr/v2
|
||||
|
||||
go 1.15
|
||||
go 1.18
|
||||
|
37
must.go
Normal file
37
must.go
Normal file
@ -0,0 +1,37 @@
|
||||
package adverr
|
||||
|
||||
// Must panics if err is not nil.
|
||||
// It returns value otherwise
|
||||
func Must[T any](value T, err error) T {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// Must2 panics if err is not nil.
|
||||
// It returns a and b otherwise
|
||||
func Must2[A, B any](a A, b B, err error) (A, B) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return a, b
|
||||
}
|
||||
|
||||
// Must3 panics if err is not nil.
|
||||
// It returns a, b and c otherwise
|
||||
func Must3[A, B, C any](a A, b B, c C, err error) (A, B, C) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return a, b, c
|
||||
}
|
||||
|
||||
// Must4 panics if err is not nil.
|
||||
// It returns a, b and c otherwise
|
||||
func Must4[A, B, C, D any](a A, b B, c C, d D, err error) (A, B, C, D) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return a, b, c, d
|
||||
}
|
101
mustify.go
Normal file
101
mustify.go
Normal 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)) }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user