// ------------------------------------------------------------------------ // 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 errs import ( "errs/fmtr" "fmt" "time" ) type ( ErrInfo struct { error message string info string desc string help string link string date time.Time metas map[string]string } ErrInfoFnc func (*ErrInfo) ) func NewErrInfo(err error, options ...ErrInfoFnc) *ErrInfo { if err == nil { return nil } return applyErrOptions(&ErrInfo{ error: err, date : time.Now(), }, options ...) } func (e ErrInfo) Info () string { return e.info } func (e ErrInfo) Date () time.Time { return e.date } func (e ErrInfo) Message () string { return e.message } func (e ErrInfo) MapMetas(f func (name string, value any) bool) { if len(e.metas) > 0 { for k,v := range e.metas { if !f(k,v) { return } } } } func (e ErrInfo) Unwrap () error { return e.error } func (e ErrInfo) WrapWithCause(cause error) error { return WrapWithCause(e,cause) } func (e ErrInfo) WrapWithTrace(offset int) error { return WrapWithTrace(e,offset) } func (e ErrInfo) String () string { return string(G.Formatter.FormatError(e)) } func (e ErrInfo) BuildFormat(builder fmtr.Builder) { builder.Build(e,func (fb fmtr.FieldBuilder){ fb.AddFieldString("message",e.message) fb.AddFieldString("info",e.info) fb.AddFieldDate("date",e.date) if len(e.metas) > 0 { for n,v := range e.metas { fb.AddFieldString(n,v) } } }) } func applyErrOptions(Err *ErrInfo, options ...ErrInfoFnc) *ErrInfo { for _,o := range options { o(Err) } return Err } func ErrInfoWithCause (cause error) ErrInfoFnc { return func(e *ErrInfo) { if cause == nil { return } e.error = *NewErrCause(e.error,cause) } } func ErrInfoWithMessage(message string, params ...any) ErrInfoFnc { if len(params) > 0 { message = fmt.Sprintf(message,params ...) } return func(e *ErrInfo) { e.message = message } } func ErrInfoWithInfo (info string, params ...any) ErrInfoFnc { if len(params) > 0 { info = fmt.Sprintf(info,params ...) } return func(e *ErrInfo) { e.info = info } } func ErrInfoWithStackTrace (offset int) ErrInfoFnc { return func (e *ErrInfo) { e.error = *NewErrTrace(e.error,offset) } } func ErrInfoWithMeta (name string, value string) ErrInfoFnc { return func (e *ErrInfo) { if e.metas == nil { e.metas=map[string]string{name:value} return } e.metas[name]=value } }