2023-02-15 16:04:19 +01:00
|
|
|
package exerr
|
|
|
|
|
|
|
|
import (
|
2023-07-24 10:42:39 +02:00
|
|
|
"github.com/rs/xid"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
2023-08-08 13:09:15 +02:00
|
|
|
"reflect"
|
2023-07-24 10:42:39 +02:00
|
|
|
"strings"
|
2023-02-15 16:04:19 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ExErr struct {
|
|
|
|
UniqueID string `json:"uniqueID"`
|
|
|
|
|
|
|
|
Timestamp time.Time `json:"timestamp"`
|
|
|
|
Category ErrorCategory `json:"category"`
|
|
|
|
Severity ErrorSeverity `json:"severity"`
|
|
|
|
Type ErrorType `json:"type"`
|
|
|
|
|
2023-07-24 10:42:39 +02:00
|
|
|
StatusCode *int `json:"statusCode"`
|
|
|
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
WrappedErrType string `json:"wrappedErrType"`
|
2023-08-08 13:09:15 +02:00
|
|
|
WrappedErr any `json:"-"`
|
2023-07-24 10:42:39 +02:00
|
|
|
Caller string `json:"caller"`
|
2023-02-15 16:04:19 +01:00
|
|
|
|
2023-07-28 15:42:12 +02:00
|
|
|
OriginalError *ExErr `json:"originalError"`
|
2023-02-15 16:04:19 +01:00
|
|
|
|
|
|
|
Meta MetaMap `json:"meta"`
|
|
|
|
}
|
|
|
|
|
2023-07-24 10:42:39 +02:00
|
|
|
func (ee *ExErr) Error() string {
|
2023-10-19 14:16:01 +02:00
|
|
|
return ee.RecursiveMessage()
|
2023-07-24 10:42:39 +02:00
|
|
|
}
|
2023-02-15 16:04:19 +01:00
|
|
|
|
2023-07-28 15:42:12 +02:00
|
|
|
// Unwrap must be implemented so that some error.XXX methods work
|
2023-07-24 10:42:39 +02:00
|
|
|
func (ee *ExErr) Unwrap() error {
|
2023-08-08 13:09:15 +02:00
|
|
|
if ee.OriginalError == nil {
|
|
|
|
return nil // this is neccessary - otherwise we return a wrapped nil and the `x == nil` comparison fails (= panic in errors.Is and other failures)
|
|
|
|
}
|
2023-07-24 10:42:39 +02:00
|
|
|
return ee.OriginalError
|
2023-02-15 16:04:19 +01:00
|
|
|
}
|
|
|
|
|
2023-07-28 15:42:12 +02:00
|
|
|
// Is must be implemented so that error.Is(x) works
|
|
|
|
func (ee *ExErr) Is(e error) bool {
|
|
|
|
return IsFrom(ee, e)
|
|
|
|
}
|
|
|
|
|
2023-08-08 12:38:22 +02:00
|
|
|
// As must be implemented so that error.As(x) works
|
|
|
|
//
|
|
|
|
//goland:noinspection GoTypeAssertionOnErrors
|
2023-08-08 13:09:15 +02:00
|
|
|
func (ee *ExErr) As(target any) bool {
|
|
|
|
if dstErr, ok := target.(*ExErr); ok {
|
|
|
|
|
2023-08-08 12:38:22 +02:00
|
|
|
if dst0, ok := ee.contains(dstErr); ok {
|
|
|
|
dstErr = dst0
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
2023-08-08 13:09:15 +02:00
|
|
|
|
2023-08-08 12:38:22 +02:00
|
|
|
} else {
|
2023-08-08 13:09:15 +02:00
|
|
|
|
|
|
|
val := reflect.ValueOf(target)
|
|
|
|
|
|
|
|
typStr := val.Type().Elem().String()
|
|
|
|
|
|
|
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
|
|
|
if curr.Category == CatForeign && curr.WrappedErrType == typStr && curr.WrappedErr != nil {
|
|
|
|
val.Elem().Set(reflect.ValueOf(curr.WrappedErr))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 12:38:22 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 10:42:39 +02:00
|
|
|
func (ee *ExErr) Log(evt *zerolog.Event) {
|
|
|
|
evt.Msg(ee.FormatLog(LogPrintFull))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ee *ExErr) FormatLog(lvl LogPrintLevel) string {
|
|
|
|
if lvl == LogPrintShort {
|
|
|
|
|
|
|
|
msg := ee.Message
|
2024-06-01 02:40:48 +02:00
|
|
|
if msg == "" {
|
|
|
|
msg = ee.RecursiveMessage()
|
|
|
|
}
|
2023-07-24 10:42:39 +02:00
|
|
|
if ee.OriginalError != nil && ee.OriginalError.Category == CatForeign {
|
|
|
|
msg = msg + " (" + strings.ReplaceAll(ee.OriginalError.Message, "\n", " ") + ")"
|
|
|
|
}
|
|
|
|
|
|
|
|
if ee.Type != TypeWrap {
|
|
|
|
return "[" + ee.Type.Key + "] " + msg
|
|
|
|
} else {
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if lvl == LogPrintOverview {
|
|
|
|
|
|
|
|
str := "[" + ee.RecursiveType().Key + "] <" + ee.UniqueID + "> " + strings.ReplaceAll(ee.RecursiveMessage(), "\n", " ") + "\n"
|
|
|
|
|
|
|
|
indent := ""
|
|
|
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
|
|
|
indent += " "
|
|
|
|
|
|
|
|
str += indent
|
|
|
|
str += "-> "
|
|
|
|
strmsg := strings.Trim(curr.Message, " \r\n\t")
|
|
|
|
if lbidx := strings.Index(curr.Message, "\n"); lbidx >= 0 {
|
|
|
|
strmsg = strmsg[0:lbidx]
|
|
|
|
}
|
|
|
|
strmsg = langext.StrLimit(strmsg, 61, "...")
|
|
|
|
str += strmsg
|
|
|
|
str += "\n"
|
|
|
|
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
|
|
|
|
} else if lvl == LogPrintFull {
|
|
|
|
|
|
|
|
str := "[" + ee.RecursiveType().Key + "] <" + ee.UniqueID + "> " + strings.ReplaceAll(ee.RecursiveMessage(), "\n", " ") + "\n"
|
|
|
|
|
|
|
|
indent := ""
|
|
|
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
|
|
|
indent += " "
|
|
|
|
|
|
|
|
etype := ee.Type.Key
|
|
|
|
if ee.Type == TypeWrap {
|
|
|
|
etype = "~"
|
|
|
|
}
|
|
|
|
|
|
|
|
str += indent
|
|
|
|
str += "-> ["
|
|
|
|
str += etype
|
|
|
|
if curr.Category == CatForeign {
|
|
|
|
str += "|Foreign"
|
|
|
|
}
|
|
|
|
str += "] "
|
|
|
|
str += strings.ReplaceAll(curr.Message, "\n", " ")
|
|
|
|
if curr.Caller != "" {
|
|
|
|
str += " (@ "
|
|
|
|
str += curr.Caller
|
|
|
|
str += ")"
|
|
|
|
}
|
|
|
|
str += "\n"
|
|
|
|
|
|
|
|
if curr.Meta.Any() {
|
|
|
|
meta := indent + " {" + curr.Meta.FormatOneLine(240) + "}"
|
|
|
|
if len(meta) < 200 {
|
|
|
|
str += meta
|
|
|
|
str += "\n"
|
|
|
|
} else {
|
|
|
|
str += curr.Meta.FormatMultiLine(indent+" ", " ", 1024)
|
|
|
|
str += "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
|
|
|
|
} else {
|
2023-02-15 16:04:19 +01:00
|
|
|
|
2023-07-24 10:42:39 +02:00
|
|
|
return "[?[" + ee.UniqueID + "]?]"
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ee *ExErr) ShortLog(evt *zerolog.Event) {
|
2023-09-27 16:18:21 +02:00
|
|
|
ee.Meta.Apply(evt, langext.Ptr(240)).Msg(ee.FormatLog(LogPrintShort))
|
2023-02-15 16:04:19 +01:00
|
|
|
}
|
|
|
|
|
2023-07-24 10:42:39 +02:00
|
|
|
// RecursiveMessage returns the message to show
|
|
|
|
// = first error (top-down) that is not wrapping/foreign/empty
|
2023-11-10 10:16:31 +01:00
|
|
|
// = lowest level error (that is not empty)
|
|
|
|
// = fallback to self.message
|
2023-07-24 10:42:39 +02:00
|
|
|
func (ee *ExErr) RecursiveMessage() string {
|
2023-11-10 10:16:31 +01:00
|
|
|
|
|
|
|
// ==== [1] ==== first error (top-down) that is not wrapping/foreign/empty
|
|
|
|
|
2023-07-24 10:42:39 +02:00
|
|
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
|
|
|
if curr.Message != "" && curr.Category != CatWrap && curr.Category != CatForeign {
|
|
|
|
return curr.Message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 10:16:31 +01:00
|
|
|
// ==== [2] ==== lowest level error (that is not empty)
|
|
|
|
|
|
|
|
deepestMsg := ""
|
|
|
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
|
|
|
if curr.Message != "" {
|
|
|
|
deepestMsg = curr.Message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if deepestMsg != "" {
|
|
|
|
return deepestMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
// ==== [3] ==== fallback to self.message
|
|
|
|
|
2023-07-24 10:42:39 +02:00
|
|
|
return ee.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecursiveType returns the statuscode to use
|
|
|
|
// = first error (top-down) that is not wrapping/empty
|
|
|
|
func (ee *ExErr) RecursiveType() ErrorType {
|
|
|
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
|
|
|
if curr.Type != TypeWrap {
|
|
|
|
return curr.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback to self
|
|
|
|
return ee.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecursiveStatuscode returns the HTTP Statuscode to use
|
|
|
|
// = first error (top-down) that has a statuscode set
|
|
|
|
func (ee *ExErr) RecursiveStatuscode() *int {
|
|
|
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
|
|
|
if curr.StatusCode != nil {
|
|
|
|
return langext.Ptr(*curr.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecursiveCategory returns the ErrorCategory to use
|
|
|
|
// = first error (top-down) that has a statuscode set
|
|
|
|
func (ee *ExErr) RecursiveCategory() ErrorCategory {
|
|
|
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
|
|
|
if curr.Category != CatWrap {
|
|
|
|
return curr.Category
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback to <empty>
|
|
|
|
return ee.Category
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:40:47 +02:00
|
|
|
// RecursiveMeta searches (top-down) for teh first error that has a meta value with teh specified key
|
|
|
|
// and returns its value (or nil)
|
|
|
|
func (ee *ExErr) RecursiveMeta(key string) *MetaValue {
|
|
|
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
|
|
|
if metaval, ok := curr.Meta[key]; ok {
|
|
|
|
return langext.Ptr(metaval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-08 12:38:22 +02:00
|
|
|
// Depth returns the depth of recursively contained errors
|
2023-07-24 10:42:39 +02:00
|
|
|
func (ee *ExErr) Depth() int {
|
|
|
|
if ee.OriginalError == nil {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return ee.OriginalError.Depth() + 1
|
|
|
|
}
|
|
|
|
}
|
2023-02-15 16:04:19 +01:00
|
|
|
|
2024-01-05 10:43:39 +01:00
|
|
|
// GetMeta returns the meta value with the specified key
|
|
|
|
// this method recurses through all wrapped errors and returns the first matching meta value
|
|
|
|
func (ee *ExErr) GetMeta(key string) (any, bool) {
|
|
|
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
|
|
|
if v, ok := curr.Meta[key]; ok {
|
|
|
|
return v.Value, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2024-01-05 10:59:06 +01:00
|
|
|
// GetMetaString functions the same as GetMeta, but returns false if the type does not match
|
|
|
|
func (ee *ExErr) GetMetaString(key string) (string, bool) {
|
|
|
|
if v1, ok := ee.GetMeta(key); ok {
|
|
|
|
if v2, ok := v1.(string); ok {
|
|
|
|
return v2, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ee *ExErr) GetMetaBool(key string) (bool, bool) {
|
|
|
|
if v1, ok := ee.GetMeta(key); ok {
|
|
|
|
if v2, ok := v1.(bool); ok {
|
|
|
|
return v2, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ee *ExErr) GetMetaInt(key string) (int, bool) {
|
|
|
|
if v1, ok := ee.GetMeta(key); ok {
|
|
|
|
if v2, ok := v1.(int); ok {
|
|
|
|
return v2, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ee *ExErr) GetMetaFloat32(key string) (float32, bool) {
|
|
|
|
if v1, ok := ee.GetMeta(key); ok {
|
|
|
|
if v2, ok := v1.(float32); ok {
|
|
|
|
return v2, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ee *ExErr) GetMetaFloat64(key string) (float64, bool) {
|
|
|
|
if v1, ok := ee.GetMeta(key); ok {
|
|
|
|
if v2, ok := v1.(float64); ok {
|
|
|
|
return v2, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ee *ExErr) GetMetaTime(key string) (time.Time, bool) {
|
|
|
|
if v1, ok := ee.GetMeta(key); ok {
|
|
|
|
if v2, ok := v1.(time.Time); ok {
|
|
|
|
return v2, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Time{}, false
|
|
|
|
}
|
|
|
|
|
2023-08-08 12:38:22 +02:00
|
|
|
// contains test if the supplied error is contained in this error (anywhere in the chain)
|
|
|
|
func (ee *ExErr) contains(original *ExErr) (*ExErr, bool) {
|
|
|
|
if original == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if ee == original {
|
|
|
|
return ee, true
|
|
|
|
}
|
|
|
|
|
|
|
|
for curr := ee; curr != nil; curr = curr.OriginalError {
|
|
|
|
if curr.equalsDirectProperties(curr) {
|
|
|
|
return curr, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// equalsDirectProperties tests if ee and other are equals, but only looks at primary properties (not `OriginalError` or `Meta`)
|
|
|
|
func (ee *ExErr) equalsDirectProperties(other *ExErr) bool {
|
|
|
|
|
|
|
|
if ee.UniqueID != other.UniqueID {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ee.Timestamp != other.Timestamp {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ee.Category != other.Category {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ee.Severity != other.Severity {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ee.Type != other.Type {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ee.StatusCode != other.StatusCode {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ee.Message != other.Message {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ee.WrappedErrType != other.WrappedErrType {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if ee.Caller != other.Caller {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-07-24 10:42:39 +02:00
|
|
|
func newID() string {
|
|
|
|
return xid.New().String()
|
2023-02-15 16:04:19 +01:00
|
|
|
}
|