// ------------------------------------------------------------------------ // Project active2 // Active Thing (activething.com) git.activething.com/go // // File name errMeta.go // Created by DEV // Modified 24/03/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 wrap import ( "bfrs" "blds" "errs/erstfy" "flds" "time" ) type ( Info struct { error Message string Info string Desc string Help string Link string Date time.Time } InfoFnc func (*Info) ) func WithInfo (err error, options ...InfoFnc) *Info { if err == nil { panic(G.ErrNilWrap.WithMessageF(errNilWrapMsg,"Info")) } ei := &Info{ error: err, Link: G.InfoLink, Date: time.Now(), } for _,o := range options { o(ei) } return ei } func (e Info) Unwrap () error { return e.error } func (e Info) WithMessage(msg string) *Message { return WithMessage(e,msg) } func (e Info) WithMessageF (msgf string, params ...any) *Message { return WithMessageF(e,msgf,params...) } func (e Info) WithFieldString (name, value string) *Field { return WithFieldString(e,name,value) } func (e Info) WithCause (cause error) *Cause { return WithCause(e,cause) } func (e Info) WithFields(fields ...flds.Field) *Fields { return WithFields(e,fields...) } func (e Info) WithTrace (offset int) *Trace { return WithTrace(e,offset) } func (e Info) Stringify (partial bool) string { b := bfrs.AcquireABuffer() sy := erstfy.DefaultStringer() if len(e.Message) > 0 { b.WriteString(sy.LabelLine("message",e.Message)) } if len(e.Info) > 0 { b.WriteString(sy.LabelLine("info",e.Info)) } if len(e.Desc) > 0 { b.WriteString(sy.LabelLine("desc",e.Desc)) } if len(e.Link) > 0 { b.WriteString(sy.LabelLine("link",e.Link)) } if len(e.Help) > 0 { b.WriteString(sy.LabelLine("help",e.Help)) } b.WriteString(sy.LabelLine("date",e.Date.String())) ln := b.String() bfrs.ReleaseABuffer(b) if partial { return b.String() } return erstfy.DefaultStringer().Stringify(e.error,ln) } func (e Info) Build (builder blds.Builder) { builder.Object( func(b blds.ObjectBuilder) { e.BuildField(b) b.field("error").Error(e.error) }) } func (e Info) BuildField (builder blds.ObjectBuilder) { builder.field("message").String(e.Message) builder.field("info").String(e.Info) builder.field("desc").String(e.Desc) builder.field("link").String(e.Link) builder.field("help").String(e.Help) builder.field("date").Time(e.Date) }