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/fmt_text_state.go

205 lines
4.1 KiB

// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name format_text_builder.go
// Created by DEV
// Modified 30/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"
"reflect"
"time"
)
type (
fmtTextState struct {
f *FmtText
buffer bytes.Buffer
level int
error error
}
)
func newFmtTextState(f *FmtText) *fmtTextState {
return &fmtTextState{ f : f } }
func (s *fmtTextState) Reset () {
s.buffer.Reset()
s.level = 0
s.error = nil
}
func (s *fmtTextState) Level () int {
return s.level }
func (s *fmtTextState) Error () error {
return s.error }
func (s *fmtTextState) Bytes () []byte {
return s.buffer.Bytes() }
func (s *fmtTextState) AddError (err error) {
if err == nil {
return
}
}
func (s *fmtTextState) AddCause (err error) {
if err == nil || !s.f.ShowCause() {
return
}
}
func (s *fmtTextState) AddTrace (trace StackTrace) {
if trace == nil || !s.f.ShowTrace() {
return
}
}
func (s *fmtTextState) AddWrapped(err error) {
if err == nil || !s.f.ShowWrapped() {
return
}
}
func (s *fmtTextState) AddCode (code string) {
s.writeField("error", s.f.ValueTransformers.Apply(code)) }
func (s *fmtTextState) AddType (ter string ) {
s.writeField("error-type",ter) }
func (s *fmtTextState) AddString (str string) {
s.writeString(s.f.StringTransformers.Apply(str))
}
func (s *fmtTextState) AddStrings(str []string) {
for _,st := range str {
s.writeString(s.f.StringTransformers.Apply(st))
}
}
func (s *fmtTextState) AddFieldString (name string, value string ) {
value = s.f.ValueTransformers.Apply(value)
if len(value) == 0 && !s.f.ShowFieldEmpty() {
return
}
s.writeField(name,value) }
func (s *fmtTextState) AddFieldInt (name string, value int ) {
s.writeField(name,fmt.Sprintf(s.f.IntLayout,value)) }
func (s *fmtTextState) AddFieldFloat (name string, value float64 ) {
s.writeField(name,fmt.Sprintf(s.f.FloatLayout,value)) }
func (s *fmtTextState) AddFieldTime (name string, value time.Time) {
s.writeField(name,value.Format(s.f.TimeLayout)) }
func (s *fmtTextState) AddFieldDate (name string, value time.Time) {
s.writeField(name,value.Format(s.f.DateLayout)) }
func (s *fmtTextState) writeField (name string, value string) {
name = s.f.NameTransformers.Apply (name)
if !s.f.ShowColors() {
s.writeString(name)
s.writeString(s.f.FieldSep)
s.writeString(value)
s.buffer.WriteString("\n")
}
}
func (s *fmtTextState) writeString(str string) {
s.buffer.WriteString(str)
}
func (s *fmtTextState) format (er error) {
dn := false
defer func(d *bool, s *fmtTextState){
if !(*d) {
if s.error == nil {
s.error = NewErrCause(G.ErrFormat,NewErrPanic(recover()))
} else {
s.error = NewErrCause(G.ErrFormat,s.error)
}
} else if s.level != 0 {
s.error = G.ErrFormatLevel
}
if s.error != nil {
panic(s.error)
}
}(&dn, s)
if fm,is := er.(Formattable); is {
fm.BuildFormat(s)
} else {
s.build(er)
}
dn=true
}
func (s *fmtTextState) build (er error) {
rt := reflect.TypeOf(er)
if rt.Kind() == reflect.Pointer {
er = reflect.ValueOf(er).Elem().Interface().(error)
if er == nil {
return
}
rt = rt.Elem()
}
s.AddCode(er.Error())
s.AddType(rt.String())
Extract(s,er)
s.AddWrapped(Unwrap(er))
s.AddCause (UnwrapCause(er))
s.AddTrace (UnwrapTrace(er))
}

Powered by TurnKey Linux.