// ------------------------------------------------------------------------ // 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 efmt import ( "reflect" "sync" ) 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" fmtFieldFormatError string = "error.%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 { builder.AddHead(err.Error(),reflect.TypeOf(err).String()) var eb FieldCollectors g.mux.RLock() defer g.mux.RUnlock() for _,c := range g.collectors { if c.CollectField(builder,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 FieldCollectors{ } }