You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
2.9 KiB
138 lines
2.9 KiB
// ------------------------------------------------------------------------
|
|
// Project active2
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File Name error_data.go
|
|
// Created by DEV
|
|
// Modified 20/04/2024
|
|
//
|
|
// Copyright 2024 activething.com
|
|
// ------------------------------------------------------------------------
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
// ------------------------------------------------------------------------
|
|
|
|
package collects
|
|
|
|
import (
|
|
"errors"
|
|
"errs/stack"
|
|
"fmt"
|
|
"reflect"
|
|
)
|
|
|
|
type (
|
|
|
|
|
|
ErrorCollection struct {
|
|
Err error
|
|
ErrStr string
|
|
Types []reflect.Type
|
|
TypesStr []string
|
|
Fields Fields
|
|
|
|
Causes []ErrorCollection
|
|
Trace stack.Trace
|
|
|
|
Invalid error
|
|
}
|
|
)
|
|
|
|
|
|
func NewErrorCollection(err error) *ErrorCollection {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
return &ErrorCollection{ Err: err }
|
|
}
|
|
|
|
|
|
func (c *ErrorCollection) Collect (addFields,addCauses,addTrace bool) *ErrorCollection {
|
|
c.collect(addFields,addCauses,addTrace)
|
|
return c
|
|
}
|
|
|
|
|
|
func (c *ErrorCollection) collect (addFields,addCauses,addTrace bool) *ErrorCollection {
|
|
d := false
|
|
ne := c.Err
|
|
|
|
defer func(ec *ErrorCollection, dn *bool){
|
|
if !(*dn) {
|
|
if rc := recover(); rc != nil {
|
|
if er,is := rc.(error); is {
|
|
ec.Invalid = er
|
|
} else {
|
|
ec.Invalid = fmt.Errorf("%#v",rc)
|
|
}
|
|
|
|
}
|
|
}
|
|
}(c,&d)
|
|
|
|
c.ErrStr = c.Err.Error()
|
|
|
|
for ne != nil {
|
|
rt := reflect.TypeOf(ne)
|
|
c.Types = append(c.Types,rt)
|
|
c.TypesStr = append(c.TypesStr,rt.String())
|
|
|
|
// Unwrap Fields
|
|
if addFields {
|
|
CollectErrorFields(&c.Fields,ne)
|
|
}
|
|
|
|
// Unwrap Causes
|
|
if addCauses {
|
|
if cs, is := ne.(interface{ Cause() error }); is {
|
|
if er := cs.Cause(); er != nil {
|
|
c.Causes = append(c.Causes, *NewErrorCollection(er).Collect(addFields,addCauses,false))
|
|
}
|
|
} else {
|
|
var ls []error
|
|
if cs, is := ne.(interface{ Causes() []error }); is {
|
|
ls = cs.Causes()
|
|
} else if cs, is := ne.(interface{ Unwrap() []error }); is {
|
|
ls = cs.Unwrap()
|
|
}
|
|
for _, er := range ls {
|
|
if ed := NewErrorCollection(er); ed != nil {
|
|
c.Causes = append(c.Causes, *ed.Collect(addFields,addCauses,false))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Unwrap Trace
|
|
if addTrace {
|
|
if tc, is := ne.(stack.Tracer); is {
|
|
c.Trace = tc.StackTrace()
|
|
addTrace = c.Trace == nil
|
|
}
|
|
|
|
}
|
|
|
|
// next error
|
|
ne = errors.Unwrap(ne)
|
|
}
|
|
d = true
|
|
return c
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|