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.
216 lines
4.6 KiB
216 lines
4.6 KiB
// ------------------------------------------------------------------------
|
|
// Project active2
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name active.go
|
|
// Created by DEV
|
|
// Modified 09/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 fmtr
|
|
|
|
import (
|
|
"reflect"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
|
|
const (
|
|
|
|
CodeBuilderError string = "build-format"
|
|
CodeBuilderLevelError string = "builder-invalid-level"
|
|
|
|
fmtErrorFormatUnwrap string = "cant unwrap error type %s"
|
|
fmtErrorTypeFormat string = "cant build format for error type %s"
|
|
|
|
|
|
)
|
|
|
|
const (
|
|
FieldNameError string = "error"
|
|
FieldNameCode string = "code"
|
|
FieldNameType string = "type"
|
|
FieldNameMessage string = "message"
|
|
FieldNameInfo string = "info"
|
|
FieldNameDate string = "date"
|
|
FieldNameTime string = "time"
|
|
FieldNameParam string = "param"
|
|
FieldNameSource string = "source"
|
|
FieldNameUser string = "user"
|
|
|
|
|
|
)
|
|
|
|
var (
|
|
|
|
g = struct {
|
|
mux sync.RWMutex
|
|
|
|
builders map[reflect.Type]ErrorBuilder
|
|
collectors FieldCollectors
|
|
}{
|
|
|
|
builders : make(map[reflect.Type]ErrorBuilder),
|
|
collectors : defaultCollectors(),
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetErrorTypeBuilder(rtype reflect.Type) ErrorBuilder {
|
|
g.mux.RLock()
|
|
b,_ := g.builders[rtype]
|
|
g.mux.RUnlock()
|
|
return b
|
|
}
|
|
|
|
|
|
func SetErrorTypeBuilder(rtype reflect.Type, errBuilder ErrorBuilder) {
|
|
if rtype == nil {
|
|
return
|
|
}
|
|
g.mux.Lock()
|
|
if errBuilder == nil {
|
|
delete(g.builders,rtype)
|
|
} else {
|
|
g.builders[rtype] = errBuilder
|
|
}
|
|
g.mux.Unlock()
|
|
}
|
|
|
|
|
|
func MakeErrorTypeBuilder(builder Builder, err error) ErrorBuilder {
|
|
var eb FieldCollectors
|
|
|
|
g.mux.RLock()
|
|
defer g.mux.RUnlock()
|
|
|
|
builder.Build(err, func(fb FieldBuilder){
|
|
for _,c := range g.collectors {
|
|
if c.CollectField(fb,err) {
|
|
eb = append(eb,c)
|
|
}
|
|
}
|
|
})
|
|
if len(eb) == 0 {
|
|
return BuildErrorFnc(BasicBuildError)
|
|
}
|
|
return eb
|
|
}
|
|
|
|
|
|
|
|
func AddCollector (collector FieldCollector) {
|
|
if collector == nil {
|
|
return
|
|
}
|
|
g.mux.Lock()
|
|
g.collectors = append(g.collectors,collector)
|
|
g.mux.Unlock()
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func defaultCollectors () FieldCollectors {
|
|
|
|
return []FieldCollector{
|
|
CollectFieldFnc(func (b FieldBuilder, e error ) bool {
|
|
if ms,is := e.(interface { Code() string }); is {
|
|
b.AddFieldString(FieldNameCode, ms.Code())
|
|
return true
|
|
}
|
|
return false
|
|
}),
|
|
|
|
|
|
CollectFieldFnc(func (b FieldBuilder, e error ) bool {
|
|
if ms,is := e.(interface { Code() string }); is {
|
|
b.AddFieldString(FieldNameCode, ms.Code())
|
|
return true
|
|
}
|
|
return false
|
|
}),
|
|
|
|
|
|
CollectFieldFnc(func (b FieldBuilder, e error ) bool {
|
|
if ms,is := e.(interface { Message() string }); is {
|
|
b.AddFieldString(FieldNameMessage, ms.Message())
|
|
return true
|
|
}
|
|
return false
|
|
}),
|
|
|
|
CollectFieldFnc(func (b FieldBuilder, e error ) bool {
|
|
if in,is := e.(interface { Info () string }); is {
|
|
b.AddFieldString(FieldNameInfo,in.Info())
|
|
return true
|
|
}
|
|
return false
|
|
}),
|
|
|
|
CollectFieldFnc(func (b FieldBuilder, e error ) bool {
|
|
if tm,is := e.(interface { Time() time.Time }); is {
|
|
b.AddFieldTime(FieldNameTime, tm.Time())
|
|
return true
|
|
}
|
|
return false
|
|
}),
|
|
|
|
CollectFieldFnc(func (b FieldBuilder, e error ) bool {
|
|
if tm,is := e.(interface { Date() time.Time }); is {
|
|
b.AddFieldDate(FieldNameDate, tm.Date())
|
|
return true
|
|
}
|
|
return false
|
|
}),
|
|
|
|
CollectFieldFnc(func (b FieldBuilder, e error ) bool {
|
|
if tm,is := e.(interface { Param() string }); is {
|
|
b.AddFieldString(FieldNameParam, tm.Param())
|
|
return true
|
|
}
|
|
return false
|
|
}),
|
|
|
|
CollectFieldFnc(func (b FieldBuilder, e error ) bool {
|
|
if tm,is := e.(interface { User() string }); is {
|
|
b.AddFieldString(FieldNameUser, tm.User())
|
|
return true
|
|
}
|
|
return false
|
|
}),
|
|
|
|
CollectFieldFnc(func (b FieldBuilder, e error ) bool {
|
|
if tm,is := e.(interface { Source() string }); is {
|
|
b.AddFieldString(FieldNameSource, tm.Source())
|
|
return true
|
|
}
|
|
return false
|
|
}),
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|