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.
active/core/errs/err_info.go

126 lines
2.5 KiB

// ------------------------------------------------------------------------
// 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 (
"time"
)
type (
ErrInfo struct {
error
message string
info 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) Format (builder FormatBuilder) {
o := builder.Options()
n := e.Unwrap()
if !o.ShowWrapped() {
e.formatWrapped(builder, n)
return
}
builder.AddCode(e.Error())
builder.AddType("core.errs.ErrInfo")
e.formatWrapped(builder, n)
if o.ShowCause() {
if c := UnwrapCause(n); c != nil {
builder.AddCause(c)
}
}
if o.ShowTrace() {
if t := UnwrapTrace(n); t != nil {
builder.AddTrace(t)
}
}
}
func (e ErrInfo) formatWrapped (builder FormatBuilder, next error) {
if builder.Options().ShowDetails() {
builder.AddFieldString("message",e.message)
builder.AddFieldString("info",e.info)
builder.AddFieldTime ("date",e.date)
if len(e.metas) > 0 {
for n,v := range e.metas {
builder.AddFieldString(n,v)
}
}
if next != nil {
builder.AddWrapped(next)
}
}
}

Powered by TurnKey Linux.