From 864923cee7ac29a988ede9a958a7e0a04305f74e Mon Sep 17 00:00:00 2001 From: DEV Date: Thu, 4 Apr 2024 02:32:08 +0200 Subject: [PATCH] incluidas las librerias de transformacion de strings --- app/main.go | 2 +- core/errs/active.go | 16 +- core/errs/err_panic.go | 5 +- core/errs/fmts/active.go | 13 +- core/errs/fmts/err_format.go | 12 ++ core/errs/fmts/fmt_text.go | 173 --------------- core/errs/fmts/fmt_text_color.go | 61 ------ core/errs/fmts/fmt_text_section.go | 36 ---- core/errs/fmts/fmt_text_state.go | 247 ---------------------- core/errs/fmts/{text => ftext}/active.go | 11 +- core/errs/fmts/{text => ftext}/color.go | 2 +- core/errs/fmts/{text => ftext}/fmt.go | 56 ++++- core/errs/fmts/{text => ftext}/section.go | 9 +- core/errs/fmts/{text => ftext}/state.go | 65 ++++-- core/errs/fmts/text/sections.go | 40 ---- core/errs/fmts/text/transformers.go | 37 ---- 16 files changed, 141 insertions(+), 644 deletions(-) delete mode 100644 core/errs/fmts/fmt_text.go delete mode 100644 core/errs/fmts/fmt_text_color.go delete mode 100644 core/errs/fmts/fmt_text_section.go delete mode 100644 core/errs/fmts/fmt_text_state.go rename core/errs/fmts/{text => ftext}/active.go (91%) rename core/errs/fmts/{text => ftext}/color.go (99%) rename core/errs/fmts/{text => ftext}/fmt.go (64%) rename core/errs/fmts/{text => ftext}/section.go (92%) rename core/errs/fmts/{text => ftext}/state.go (78%) delete mode 100644 core/errs/fmts/text/sections.go delete mode 100644 core/errs/fmts/text/transformers.go diff --git a/app/main.go b/app/main.go index 79b432c..680fe79 100644 --- a/app/main.go +++ b/app/main.go @@ -48,7 +48,7 @@ func main() { //fmt.Print(errs.ToFormat(ercode,nil)) //fmt.Print("\n") - erpanic := errs.NewErrPanic("error->err-mensaje-panico") + erpanic := errs.NewErrPanic(errs.PanicRecoveredError("error->err-mensaje-panico")) fmt.Print(errs.ToFormat(erpanic,nil)) fmt.Print("\n") diff --git a/core/errs/active.go b/core/errs/active.go index 664a0bb..1466b23 100644 --- a/core/errs/active.go +++ b/core/errs/active.go @@ -27,6 +27,7 @@ package errs import ( "atypes/astrings" "errs/fmts" + "errs/fmts/ftext" "errs/stack" "fmt" "reflect" @@ -63,12 +64,15 @@ var ( stackTraceRType : reflect.TypeOf(stack.Trace(nil)), - Formatter : fmts.NewFmtText(func (f *fmts.FmtText) { + Formatter : ftext.NewFmtText(func (f *ftext.FmtText) { f.FixFieldLen = 13 }, - fmts.FmtTextWithNameTransformer( + ftext.FmtTextWithNameTransformer( astrings.ToKebab, - )), + ), + ftext.FmtTextWithErrorSection ( + []string{"**********************************************************"}, + []string{"**********************************************************"},ftext.ColorMagenta)), } ) @@ -108,6 +112,12 @@ func ToString (err error) string { } +func PanicRecoveredError (rec any) error { + if rec == nil { + return nil + } + return *NewErrPanic(ToErr(rec)) +} diff --git a/core/errs/err_panic.go b/core/errs/err_panic.go index 7cef877..8999152 100644 --- a/core/errs/err_panic.go +++ b/core/errs/err_panic.go @@ -33,11 +33,12 @@ type ( ) -func NewErrPanic (err any) *ErrPanic { - return &ErrPanic{ cause: ToErr(err) } +func NewErrPanic (cause error) *ErrPanic { + return &ErrPanic{ cause: cause } } + func (e ErrPanic) Error () string { return CodePanicError } diff --git a/core/errs/fmts/active.go b/core/errs/fmts/active.go index d789b78..412079b 100644 --- a/core/errs/fmts/active.go +++ b/core/errs/fmts/active.go @@ -25,18 +25,17 @@ package fmts +const ( + + CodeBuilderError string = "format-builder-error" + CodeInvalidLevelError string = "format-invalid-level" +) var ( - G = struct { - CodeBuilderError string - CodeInvalidLevelError string + G = struct {}{} - }{ - CodeBuilderError: "format-builder-error", - CodeInvalidLevelError: "format-invalid-level", - } ) diff --git a/core/errs/fmts/err_format.go b/core/errs/fmts/err_format.go index 70d4ff0..bf8e050 100644 --- a/core/errs/fmts/err_format.go +++ b/core/errs/fmts/err_format.go @@ -38,6 +38,18 @@ func NewErrFormat (code string, cause error) *ErrFormat { } } +func NewErrFormatBuilder (cause error) *ErrFormat { + return &ErrFormat{ + code : CodeBuilderError, + cause: cause, + } +} + +func NewErrFormatLevel () *ErrFormat { + return &ErrFormat{ code : CodeInvalidLevelError } } + + + func (e ErrFormat) Code () string { return e.code } diff --git a/core/errs/fmts/fmt_text.go b/core/errs/fmts/fmt_text.go deleted file mode 100644 index 82daf06..0000000 --- a/core/errs/fmts/fmt_text.go +++ /dev/null @@ -1,173 +0,0 @@ -// ------------------------------------------------------------------------ -// Project active2 -// Active Thing (activething.com) git.activething.com/go -// -// File name format_text.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 fmts - -import ( - "atypes/astrings" - "time" -) - -const ( - - FormatShowWrapped uint8 = 1 << iota - FormatShowType - FormatShowColors - FormatShowIndent - FormatShowDetails - FormatShowCause - FormatShowTrace - FormatShowFieldEmpty -) - - - - -type ( - - FmtText struct { - Show uint8 - - Indent string - FieldSep string - - FixFieldLen int - MaxStringLen int - - TimeLayout string - DateLayout string - IntLayout string - FloatLayout string - - Color FmtTextColor - CodeColor FmtTextColor - FieldNameColor FmtTextColor - FieldValueColor FmtTextColor - - Lines [4]*fmtTextSection - - StringTransformers astrings.Transformers - ValueTransformers astrings.Transformers - NameTransformers astrings.Transformers - - builders *TypeFormatBuilders - extractors FieldExtractors - } - - FmtTextFnc func (fm *FmtText) -) - - - -func NewFmtText(options ...FmtTextFnc) *FmtText { - f := &FmtText{ - Show : FormatShowCause | FormatShowWrapped | FormatShowDetails | FormatShowDetails, - Indent : " ", - FieldSep : " : ", - - TimeLayout : time.RFC822Z, - DateLayout : time.RFC822Z, - IntLayout : "%d", - FloatLayout: "%-10s: %.2f", - - builders : NewTypeBuilders(), - extractors : DefaultsExtractors(), - } - for _,o := range options { - o(f) - } - if f.FixFieldLen > 0 { - f.NameTransformers = append(f.NameTransformers,func(s string)string { - return astrings.Pad(s,f.FixFieldLen,' ',true) - }) - } - - return f - -} - - -func (f *FmtText) Format (err error) []byte { - if err == nil { - return nil - } - - st := newFmtTextState(f) - defer st.reset() - - st.format(err) - if st.error != nil { - e := st.error - st.reset() - st.format(e) - } - - return st.buffer.Bytes() -} - - - - - -func (f *FmtText) ShowWrapped () bool { - return f.Show&FormatShowWrapped !=0 } - -func (f *FmtText) ShowType () bool { - return f.Show&FormatShowType !=0 } - -func (f *FmtText) ShowColors () bool { - return f.Show&FormatShowColors !=0 } - -func (f *FmtText) ShowIndent () bool { - return f.Show&FormatShowIndent !=0 } - -func (f *FmtText) ShowDetails() bool { - return f.Show&FormatShowDetails !=0 } - -func (f *FmtText) ShowCause () bool { - return f.Show&FormatShowCause !=0 } - -func (f *FmtText) ShowTrace () bool { - return f.Show&FormatShowTrace !=0 } - -func (f *FmtText) ShowFieldEmpty () bool { - return f.Show&FormatShowFieldEmpty !=0 } - - - - - -func FmtTextWithNameLenTransformer(len int) FmtTextFnc { - return func(f *FmtText) { - f.NameTransformers = append(f.NameTransformers,func(s string)string { - return astrings.Pad(s,len,' ',true) - }) - } -} - -func FmtTextWithNameTransformer (transformer ...astrings.Transform) FmtTextFnc { - return func(f *FmtText) { - f.NameTransformers = append(f.NameTransformers,transformer...) - } -} - diff --git a/core/errs/fmts/fmt_text_color.go b/core/errs/fmts/fmt_text_color.go deleted file mode 100644 index 99fffc4..0000000 --- a/core/errs/fmts/fmt_text_color.go +++ /dev/null @@ -1,61 +0,0 @@ -// ------------------------------------------------------------------------ -// Project active2 -// Active Thing (activething.com) git.activething.com/go -// -// File name color.go -// Created by DEV -// Modified 27/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 fmts - -import "fmt" - -const ( - - FormatColorBlack FmtTextColor = iota + 30 - FormatColorRed - FormatColorGreen - FormatColorYellow - FormatColorBlue - FormatColorMagenta - FormatColorCyan - FormatColorWhite -) - - -type ( - - FmtTextColor uint8 -) - - -func (c FmtTextColor) BeginColorString() string { - return fmt.Sprintf("\u001B[%dm",uint8(c)) -} - -func (_ FmtTextColor) EndColorString () string { - return "\x1b[0m" } - - -func (c FmtTextColor) Sprintf(fm string, params ...any) string { - return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c),fmt.Sprintf(fm,params...)) } - - -func (c FmtTextColor) IsValid() bool { - return c >= FormatColorBlack && c <= FormatColorWhite -} \ No newline at end of file diff --git a/core/errs/fmts/fmt_text_section.go b/core/errs/fmts/fmt_text_section.go deleted file mode 100644 index b73280e..0000000 --- a/core/errs/fmts/fmt_text_section.go +++ /dev/null @@ -1,36 +0,0 @@ -// ------------------------------------------------------------------------ -// Project active2 -// Active Thing (activething.com) git.activething.com/go -// -// File name fmt_lines.go -// Created by DEV -// Modified 03/04/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 fmts - - -type ( - - fmtTextSection struct { - startLines []string - endLines []string - color FmtTextColor - } -) - - diff --git a/core/errs/fmts/fmt_text_state.go b/core/errs/fmts/fmt_text_state.go deleted file mode 100644 index bf74c7d..0000000 --- a/core/errs/fmts/fmt_text_state.go +++ /dev/null @@ -1,247 +0,0 @@ -// ------------------------------------------------------------------------ -// 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 fmts - -import ( - "bytes" - "errors" - "errs/stack" - "fmt" - "reflect" - "strings" - "time" -) - - -type ( - - fmtTextState struct { - f *FmtText - buffer bytes.Buffer - level int - indent string - error error - isWrapped bool - } - -) - -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) AddError (err error) { - if err == nil { - return - } -} - - -func (s *fmtTextState) addCause(err error) { - if err == nil || !s.f.ShowCause() { - return - } - s.addLevel() - defer s.decLevel() - - s.writeString(s.indent) - s.writeString(s.f.StringOnCause) - w := s.isWrapped - if w { - s.isWrapped = false - defer func(){ - s.isWrapped = true - }() - } - s.format(err) -} - -func (s *fmtTextState) addTrace(trace stack.Trace) { - if trace == nil || !s.f.ShowTrace() { - return - } - -} - -func (s *fmtTextState) addWrapped(err error) { - if err == nil || !s.f.ShowWrapped() { - return - } - if !s.isWrapped { - s.isWrapped = true - defer func(st *fmtTextState) { s.isWrapped = false }(s) - } - s.format(err) -} - - - -func (s *fmtTextState) AddCode (code string) { - if s.isWrapped { - return - } - s.writeField("error", s.f.ValueTransformers.Apply(code)) -} - -func (s *fmtTextState) AddType (ter string ) { - if s.isWrapped { - return - } - 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) - s.writeString(fmt.Sprintf("%s%s%s%s\n",s.indent,name,s.f.FieldSep,value)) -} - - -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 { - var ie error - if re := recover(); re != nil { - if ne,is := re.(error); is { - ie = ne - } else { - ie = fmt.Errorf(G.CodeBuilderError,"%v",re) - } - } - s.error = *NewErrFormat(G.CodeBuilderError,ie) - } else { - s.error = *NewErrFormat(G.CodeBuilderError,s.error) - } - } - }(&dn, s) - - - if fm,is := er.(Formattable); is { - fm.BuildFormat(s) - } else { - rt := reflect.TypeOf(er) - if rt.Kind() == reflect.Pointer { - rt = rt.Elem() - } - - if fb := s.f.builders.TypeFormatBuilder(rt); fb != nil { - fb.BuildFormatError(s,er) - } else { - s.f.builders.Set(rt,s.f.extractors.BuildAndCreate(s,er)) - } - } - - s.addWrapped(errors.Unwrap(er)) - - if cs,is := er.(interface{ Cause() error }); is { - s.addCause(cs.Cause()) - } - - if s.level == 0 { - s.addTrace(stack.UnwrapTrace(er,true)) - } - - - dn=true -} - - - - -func (s *fmtTextState) decLevel() { - s.level-- - if s.level <= 0 { - s.indent = "" - return - } - if s.level == 1 { - s.indent = s.f.Indent - return - } - s.indent = strings.Repeat(s.f.Indent,s.level) -} - -func (s *fmtTextState) addLevel() { - s.level++ - if s.level == 1 { - s.indent = s.f.Indent - return - } - s.indent = strings.Repeat(s.f.Indent,s.level) -} - - - diff --git a/core/errs/fmts/text/active.go b/core/errs/fmts/ftext/active.go similarity index 91% rename from core/errs/fmts/text/active.go rename to core/errs/fmts/ftext/active.go index 78f7e05..3d3866d 100644 --- a/core/errs/fmts/text/active.go +++ b/core/errs/fmts/ftext/active.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package text +package ftext @@ -31,14 +31,17 @@ const ( FieldsSection CauseSection TraceSection + + secsSize= 4 ) const ( - stringsTrans int = iota - fieldNameTrans - fieldValueTrans + StringsTrans int = iota + NamesTrans + ValuesTrans + transSize int = 3 ) diff --git a/core/errs/fmts/text/color.go b/core/errs/fmts/ftext/color.go similarity index 99% rename from core/errs/fmts/text/color.go rename to core/errs/fmts/ftext/color.go index 355c069..0d65ae5 100644 --- a/core/errs/fmts/text/color.go +++ b/core/errs/fmts/ftext/color.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package text +package ftext import "fmt" diff --git a/core/errs/fmts/text/fmt.go b/core/errs/fmts/ftext/fmt.go similarity index 64% rename from core/errs/fmts/text/fmt.go rename to core/errs/fmts/ftext/fmt.go index f34dad3..c104986 100644 --- a/core/errs/fmts/text/fmt.go +++ b/core/errs/fmts/ftext/fmt.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package text +package ftext import ( "atypes/astrings" @@ -65,11 +65,11 @@ type ( FieldNameColor Color FieldValueColor Color - sections sections - transformers transformers + secs [secsSize]section + trans [transSize]astrings.Transformers builders *fmts.TypeFormatBuilders - extractors *fmts.FieldExtractors + extractors fmts.FieldExtractors } FmtTextFnc func (fm *FmtText) @@ -89,19 +89,18 @@ func NewFmtText(options ...FmtTextFnc) *FmtText { FloatLayout: "%-10s: %.2f", builders : fmts.NewTypeBuilders(), - extractors : DefaultsExtractors(), + extractors : fmts.DefaultsExtractors(), } for _,o := range options { o(f) } if f.FixFieldLen > 0 { - f.transformers.fieldName = append(f.transformers.fieldName,func(s string)string { + f.trans[NamesTrans] = append(f.trans[NamesTrans],func(s string)string { return astrings.Pad(s,f.FixFieldLen,' ',true) }) } return f - } @@ -111,13 +110,19 @@ func (f *FmtText) Format (err error) []byte { } st := newFmtTextState(f) - defer st.reset() + defer func (s *fmtTextState) { + s.reset() + }(st) st.format(err) + if st.error != nil { e := st.error st.reset() st.format(e) + } else if st.level != 0 { + st.reset() + st.format(*fmts.NewErrFormatLevel()) } return st.buffer.Bytes() @@ -155,17 +160,48 @@ func (f *FmtText) ShowFieldEmpty () bool { + func FmtTextWithNameLenTransformer(len int) FmtTextFnc { return func(f *FmtText) { - f.transformers.fieldName = append(f.transformers.fieldName,func(s string)string { + f.trans[NamesTrans] = append(f.trans[NamesTrans],func(s string)string { return astrings.Pad(s,len,' ',true) }) } } + +func FmtTextWithStringTransformer(transformer ...astrings.Transform) FmtTextFnc { + return func(f *FmtText) { + f.trans[StringsTrans] = append(f.trans[StringsTrans], transformer...) }} + func FmtTextWithNameTransformer (transformer ...astrings.Transform) FmtTextFnc { return func(f *FmtText) { - f.transformers.fieldName = append(f.transformers.fieldName,transformer...) + f.trans[NamesTrans] = append(f.trans[NamesTrans], transformer...) }} + +func FmtTextWithValueTransformer (transformer ...astrings.Transform) FmtTextFnc { + return func(f *FmtText) { + f.trans[ValuesTrans] = append(f.trans[ValuesTrans], transformer...) }} + + +func FmtTextWithErrorSection (beginLines, endLines []string, color Color) FmtTextFnc { + return FmtTextWithSection (ErrorSection,beginLines,endLines,color) } + +func FmtTextWithFieldsSection (beginLines, endLines []string, color Color) FmtTextFnc { + return FmtTextWithSection (FieldsSection,beginLines,endLines,color) } + +func FmtTextWithCauseSection (beginLines, endLines []string, color Color) FmtTextFnc { + return FmtTextWithSection (CauseSection,beginLines,endLines,color) } + +func FmtTextWithTraceSection (beginLines, endLines []string, color Color) FmtTextFnc { + return FmtTextWithSection (TraceSection,beginLines,endLines,color) } + + +func FmtTextWithSection (id int, beginLines, endLines []string, color Color) FmtTextFnc { + return func(f *FmtText) { + f.secs[id].beginLines = append([]string(nil), beginLines...) + f.secs[id].endLines = append([]string(nil), endLines ...) + f.secs[id].color = color } } + diff --git a/core/errs/fmts/text/section.go b/core/errs/fmts/ftext/section.go similarity index 92% rename from core/errs/fmts/text/section.go rename to core/errs/fmts/ftext/section.go index 9aa32e3..df5091d 100644 --- a/core/errs/fmts/text/section.go +++ b/core/errs/fmts/ftext/section.go @@ -21,16 +21,15 @@ // limitations under the License. // ------------------------------------------------------------------------ -package text +package ftext type ( section struct { - startLines []string - endLines []string - color Color + beginLines []string + endLines []string + color Color } ) - diff --git a/core/errs/fmts/text/state.go b/core/errs/fmts/ftext/state.go similarity index 78% rename from core/errs/fmts/text/state.go rename to core/errs/fmts/ftext/state.go index 131b890..12ead4e 100644 --- a/core/errs/fmts/text/state.go +++ b/core/errs/fmts/ftext/state.go @@ -21,11 +21,12 @@ // limitations under the License. // ------------------------------------------------------------------------ -package text +package ftext import ( "bytes" "errors" + "errs/fmts" "errs/stack" "fmt" "reflect" @@ -54,13 +55,14 @@ func newFmtTextState(f *FmtText) *fmtTextState { func (s *fmtTextState) reset() { s.buffer.Reset() + s.isWrapped = false s.level = 0 s.error = nil } -func (s *fmtTextState) AddError (err error) { +func (s *fmtTextState) addError (err error) { if err == nil { return } @@ -74,8 +76,7 @@ func (s *fmtTextState) addCause(err error) { s.addLevel() defer s.decLevel() - s.writeString(s.indent) - s.writeString(s.f.StringOnCause) + s.writeSection(CauseSection,true) w := s.isWrapped if w { s.isWrapped = false @@ -84,6 +85,7 @@ func (s *fmtTextState) addCause(err error) { }() } s.format(err) + s.writeSection(CauseSection,false) } func (s *fmtTextState) addTrace(trace stack.Trace) { @@ -91,6 +93,8 @@ func (s *fmtTextState) addTrace(trace stack.Trace) { return } + s.writeSection(TraceSection,true) + s.writeSection(TraceSection,false) } func (s *fmtTextState) addWrapped(err error) { @@ -110,7 +114,7 @@ func (s *fmtTextState) AddCode (code string) { if s.isWrapped { return } - s.writeField("error", s.f.ValueTransformers.Apply(code)) + s.writeField("error", s.f.trans[ValuesTrans].Apply(code)) } func (s *fmtTextState) AddType (ter string ) { @@ -124,18 +128,18 @@ func (s *fmtTextState) AddType (ter string ) { func (s *fmtTextState) AddString (str string) { - s.writeString(s.f.StringTransformers.Apply(str)) + s.writeString(s.f.trans[StringsTrans].Apply(str)) } func (s *fmtTextState) AddStrings(str []string) { for _,st := range str { - s.writeString(s.f.StringTransformers.Apply(st)) + s.writeString(s.f.trans[StringsTrans].Apply(st)) } } func (s *fmtTextState) AddFieldString (name string, value string ) { - value = s.f.ValueTransformers.Apply(value) + value = s.f.trans[ValuesTrans].Apply(value) if len(value) == 0 && !s.f.ShowFieldEmpty() { return } @@ -155,11 +159,31 @@ func (s *fmtTextState) AddFieldDate (name string, value time.Time) { func (s *fmtTextState) writeField (name string, value string) { - name = s.f.NameTransformers.Apply (name) + name = s.f.trans[NamesTrans].Apply (name) s.writeString(fmt.Sprintf("%s%s%s%s\n",s.indent,name,s.f.FieldSep,value)) } + +func (s *fmtTextState) writeSection(sec int, begin bool) { + sc := s.f.secs[sec] + var ls []string + if begin { + ls = sc.beginLines + } else { + ls = sc.endLines + } + if len(ls) > 0 { + for _,l := range ls { + s.writeString(l) + s.buffer.WriteByte('\n') + } + } +} + + + + func (s *fmtTextState) writeString(str string) { s.buffer.WriteString(str) } @@ -167,29 +191,35 @@ func (s *fmtTextState) writeString(str string) { + + func (s *fmtTextState) format (er error) { dn := false defer func(d *bool, s *fmtTextState){ if !(*d) { if s.error == nil { - var ie error + var ne error if re := recover(); re != nil { - if ne,is := re.(error); is { - ie = ne - } else { - ie = fmt.Errorf(G.CodeBuilderError,"%v",re) + var is bool + if ne,is = re.(error); !is { + ne = fmt.Errorf("%#v",re) } } - s.error = *NewErrFormat(G.CodeBuilderError,ie) + s.error = *fmts.NewErrFormatBuilder(ne) } else { - s.error = *NewErrFormat(G.CodeBuilderError,s.error) + s.error = *fmts.NewErrFormatBuilder(s.error) } } }(&dn, s) + s.writeString(fmt.Sprintf("Level %d\n",s.level)) + if s.level == 0 && !s.isWrapped { + s.writeSection(ErrorSection,true) + } + - if fm,is := er.(Formattable); is { + if fm,is := er.(fmts.Formattable); is { fm.BuildFormat(s) } else { rt := reflect.TypeOf(er) @@ -212,6 +242,7 @@ func (s *fmtTextState) format (er error) { if s.level == 0 { s.addTrace(stack.UnwrapTrace(er,true)) + s.writeSection(ErrorSection,false) } diff --git a/core/errs/fmts/text/sections.go b/core/errs/fmts/text/sections.go deleted file mode 100644 index 54e565a..0000000 --- a/core/errs/fmts/text/sections.go +++ /dev/null @@ -1,40 +0,0 @@ -// ------------------------------------------------------------------------ -// Project active2 -// Active Thing (activething.com) git.activething.com/go -// -// File name sections.go -// Created by DEV -// Modified 04/04/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 text - - -type ( - - sections struct { - err *section - fields *section - cause *section - trace *section - } - -) - - - - diff --git a/core/errs/fmts/text/transformers.go b/core/errs/fmts/text/transformers.go deleted file mode 100644 index c78fa04..0000000 --- a/core/errs/fmts/text/transformers.go +++ /dev/null @@ -1,37 +0,0 @@ -// ------------------------------------------------------------------------ -// Project active2 -// Active Thing (activething.com) git.activething.com/go -// -// File name transformers.go -// Created by DEV -// Modified 04/04/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 text - -import "atypes/astrings" - -type ( - - transformers struct { - strings astrings.Transformers - fieldName astrings.Transformers - fieldValue astrings.Transformers - } -) - -