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

131 lines
2.8 KiB

// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name err_info.go
// Created by DEV
// Modified 21/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 (
"bytes"
"fmt"
"time"
)
type (
// ErrInfo
// Representa un objeto para añadir una capa de información addicional a un error
//
ErrInfo struct {
/*embedded*/ error
date time.Time
help string
message string
fields map[string]any
stack Stack
}
ErrInfoFnc func (info *ErrInfo)
)
func NewErrInfo (err error, options ...ErrInfoFnc) *ErrInfo {
if err == nil {
return nil
}
return applyErrInfoOptions(&ErrInfo{ error: err }, options ...)
}
func (e ErrInfo) Cause() error {
if cs,is := e.error.(Causer); is {
return cs.Cause()
}
return Unwrap(e.error)
}
func (e ErrInfo) Date () time.Time {
return e.date }
func (e ErrInfo) Message() string {
return e.message }
func (e ErrInfo) Help () string {
return e.help }
func (e ErrInfo) Stack () Stack {
return e.stack }
func (e ErrInfo) Fields () map[string]any {
return e.fields }
func (e ErrInfo) String () string {
bf := bytes.Buffer{}
defer bf.Reset()
bf.WriteString("Error **************************************************\n")
bf.WriteString("Source : ")
bf.WriteString(e.error.Error())
if e.Cause() != nil {
bf.WriteString("\nCause : ")
bf.WriteString(e.Cause().Error())
}
if len(e.message) > 0 {
bf.WriteString("\nMessage: ")
bf.WriteString(e.message)
}
if !e.date.IsZero() {
bf.WriteString("\nDate : ")
bf.WriteString(e.date.Format(time.RFC850))
}
if len(e.help) > 0 {
bf.WriteString("\nHelp : ")
bf.WriteString(e.message)
}
if len(e.fields) > 0 {
bf.WriteString(fmt.Sprintf("\nFields : (%d)",len(e.fields)))
for f, v := range e.fields {
bf.WriteString(fmt.Sprintf("\n - %s: %v",f,v))
}
}
if e.stack != nil {
bf.WriteString("\nStack --------------------------------------------------\n")
bf.WriteString(e.stack.String())
bf.WriteString("\n--------------------------------------------------------\n")
}
return bf.String()
}

Powered by TurnKey Linux.