adverr/error.go

166 lines
3.4 KiB
Go
Raw Normal View History

2020-09-09 11:48:46 +02:00
package adverr
import (
"errors"
"reflect"
"strings"
)
// Error is a wrapper for error with stack trace
type Error struct {
msg string
callStack *CallStack
2020-09-09 11:48:46 +02:00
tmpl *ErrTmpl
cause error
2021-09-09 16:35:48 +02:00
prev []error
2020-09-09 11:48:46 +02:00
}
// New returns a new Error with the given message
func New(msg string) *Error {
return &Error{
msg: msg,
callStack: Trace(2),
2020-09-09 11:48:46 +02:00
}
}
// Wrap returns a new Error with the given message which is caused by cause
func Wrap(msg string, cause error) *Error {
return &Error{
msg: msg,
cause: cause,
callStack: Trace(2),
2020-09-09 11:48:46 +02:00
}
}
2021-09-09 16:35:48 +02:00
// Chain returns a new Error with the given message and a slice of errors
// which were caused in the same function in succession
func Chain(msg string, errors []error) *Error {
return &Error{
msg: msg,
callStack: Trace(2),
2021-09-09 16:35:48 +02:00
prev: errors,
}
}
2020-09-09 11:48:46 +02:00
func errtype(err error) string {
if e, ok := err.(*Error); ok && e.tmpl != nil {
return errtype(e.tmpl)
} else if tmpl, ok := err.(*ErrTmpl); ok {
2020-09-09 13:34:23 +02:00
return tmpl.name
2020-09-09 11:48:46 +02:00
}
t := reflect.TypeOf(err)
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t.PkgPath() + "." + t.Name()
}
func (e *Error) Unwrap() error {
return e.cause
}
func (e *Error) Error() string {
b := new(strings.Builder)
printErr(e, b)
return b.String()
}
2023-07-27 17:12:16 +02:00
func (e *Error) Message() string {
return e.msg
}
func (e *Error) Stack() *CallStack {
return e.callStack
}
2020-09-09 11:48:46 +02:00
// 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 {
// same error instance
if target == e {
return true
}
// no template used, therefore no equality possible
if e.tmpl == nil {
return false
}
// same template, therefore errors are equal to another
if tErr, ok := target.(*Error); ok {
return tErr.tmpl == e.tmpl
}
2020-10-19 11:40:45 +02:00
// target is the template itself, therefore they are considered equal to another
if tTmpl, ok := target.(*ErrTmpl); ok {
return tTmpl == e.tmpl
}
2020-09-09 11:48:46 +02:00
return false
}
2021-09-09 16:35:48 +02:00
// Get Returns the first error in the chain for which errors.Is(target) returns true
func (e *Error) Get(target error) error {
if e.prev == nil {
return nil
}
for _, prevErr := range e.prev {
if errors.Is(prevErr, target) {
return prevErr
}
}
return nil
}
// GetByIndex returns the i'th error in the chain
func (e *Error) GetByIndex(i int) error {
if e.prev == nil {
return nil
}
return e.prev[i]
}
// Contains is a shorthand for Get(target) != nil.
// Can be considered as an errors.Is function but for chains instead of causes
func (e *Error) Contains(target error) bool {
return e.Get(target) != nil
}
// Chain returns a slice of all chained errors
func (e *Error) Chain() []error {
return e.prev[:]
}
2020-09-09 11:48:46 +02:00
func printErr(err error, b *strings.Builder) {
2021-09-09 16:35:48 +02:00
e, ok := err.(*Error)
if ok {
2020-09-09 11:48:46 +02:00
b.WriteString(errtype(e))
b.WriteString(": ")
b.WriteString(e.msg)
b.WriteString("\n")
b.WriteString(e.callStack.String())
2020-09-09 11:48:46 +02:00
} else {
b.WriteString(errtype(err))
b.WriteString(": ")
b.WriteString(err.Error())
2021-09-09 16:35:48 +02:00
b.WriteString("\n")
b.WriteString("\t(Unknown source)\n")
2020-09-09 11:48:46 +02:00
}
cause := errors.Unwrap(err)
if cause != nil {
b.WriteString("Caused by ")
printErr(cause, b)
}
2021-09-09 16:35:48 +02:00
if ok {
for _, prevErr := range e.prev {
b.WriteString("Previously thrown ")
printErr(prevErr, b)
}
}
2020-09-09 11:48:46 +02:00
}