parent
fbc7806c34
commit
d43d13c37f
@ -0,0 +1,44 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name frame_format_builder.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 err_fmt
|
||||
|
||||
import "errs/stack"
|
||||
|
||||
type (
|
||||
|
||||
FrameBuilder interface {
|
||||
|
||||
BuildFrame (builder Builder, frame stack.Frame, num int)
|
||||
}
|
||||
|
||||
BuildFrameFnc func (builder Builder, frame stack.Frame, num int)
|
||||
|
||||
)
|
||||
|
||||
|
||||
func (f BuildFrameFnc) BuildFrame (builder Builder, frame stack.Frame, num int) {
|
||||
f(builder, frame, num) }
|
||||
|
||||
|
||||
|
||||
@ -1,178 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name state.go
|
||||
// Created by DEV
|
||||
// Modified 09/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 err_fmt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errs/stack"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
state struct {
|
||||
f TextFmt
|
||||
b bytes.Buffer
|
||||
c
|
||||
doUnwrap bool
|
||||
doCause bool
|
||||
doTrace bool
|
||||
trace []byte
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func newState (fmt TextFmt) *state {
|
||||
return &state {
|
||||
f : fmt,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (s *state) AddHead(code, errType string) {
|
||||
|
||||
}
|
||||
|
||||
func (s *state) AddFieldString (name string, value string ) {}
|
||||
|
||||
func (s *state) AddFieldInt (name string, value int ) {}
|
||||
|
||||
func (s *state) AddFieldBool (name string, value bool ) {}
|
||||
|
||||
func (s *state) AddFieldFloat (name string, value float64 ) {}
|
||||
|
||||
func (s *state) AddFieldTime (name string, value time.Time) {}
|
||||
|
||||
func (s *state) AddFieldDate (name string, value time.Time) {}
|
||||
|
||||
|
||||
func (s *state) AddString(str string) {}
|
||||
|
||||
|
||||
|
||||
|
||||
func (s *state) AddCause (cause error) {
|
||||
if cause == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (s *state) addTrace (trace stack.Trace) {
|
||||
if trace == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (s *state) format (err error, wrapped bool, level int) (fe error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
d := false
|
||||
defer func (st *state, dn *bool, er error){
|
||||
if !(*dn) {
|
||||
if rc := recover(); fe != nil {
|
||||
ec,is := rc.(error)
|
||||
if !is {
|
||||
ec = fmt.Errorf("%#v",rc)
|
||||
}
|
||||
fe = *NewErrFormatBuilder(ec,er,true)
|
||||
}
|
||||
}
|
||||
}(s,&d,err)
|
||||
|
||||
if fb,is := err.(Buildable); is {
|
||||
fb.Build(s)
|
||||
} else {
|
||||
rt := reflect.TypeOf(err)
|
||||
//if rt.Kind() == reflect.Pointer {
|
||||
// rt = rt.Elem()
|
||||
//}
|
||||
if eb := GetErrorBuilder(rt); eb != nil {
|
||||
eb.BuildError(s,err)
|
||||
} else {
|
||||
eb = MakeErrorBuilder(s,err)
|
||||
SetErrorBuilder(rt,eb)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if s.doUnwrap {
|
||||
if uw,is := err.(interface { Unwrap() error }); is {
|
||||
if ew := s.format(uw.Unwrap(), true, level); ew != nil {
|
||||
// añadimos un field fmt-(nombre del tipo de error) con el code
|
||||
// o mensaje de error
|
||||
//todo
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MultiErr
|
||||
if ua,is := err.(interface { Unwrap() []error }); is {
|
||||
for _,el := range ua.Unwrap() {
|
||||
if me := s.format(el,false,level); me != nil {
|
||||
// añadimos un field fmt-(nombre del tipo de error) con el code
|
||||
// o mensaje de error
|
||||
//todo
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if s.doCause {
|
||||
if cs, is := err.(interface{ Cause() error }); is {
|
||||
if ec := s.format(cs.Cause(), false, level+1); ec != nil {
|
||||
// añadimos un field fmt-(nombre del tipo de error) con el code
|
||||
// o mensaje de error
|
||||
//todo
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if s.doTrace {
|
||||
if tc, is := err.(interface{ stack.Tracer }); is {
|
||||
s.addTrace(tc.StackTrace())
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,54 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name text_fmt_section.go
|
||||
// Created by DEV
|
||||
// Modified 10/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 err_fmt
|
||||
|
||||
import "ansi"
|
||||
|
||||
type (
|
||||
|
||||
textFmtSection struct {
|
||||
ansi ansi.Code
|
||||
beginFn BuildFnc
|
||||
endFn BuildFnc
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func (s *textFmtSection) begin (b Builder) {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
if s.beginFn != nil {
|
||||
s.beginFn(b)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *textFmtSection) end (b Builder) {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
if s.endFn != nil {
|
||||
s.endFn(b)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,215 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name state.go
|
||||
// Created by DEV
|
||||
// Modified 09/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 err_fmt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errs/stack"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
state struct {
|
||||
f TextFmt
|
||||
b bytes.Buffer
|
||||
doHead bool
|
||||
doFields bool
|
||||
doUnwrap bool
|
||||
doCause bool
|
||||
doTrace bool
|
||||
|
||||
trace []byte
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func newState (fmt TextFmt) *state {
|
||||
return &state {
|
||||
f : fmt,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (s *state) AddHead(code, errType string) {
|
||||
if s.doHead {
|
||||
s.doHead = false
|
||||
s.doFields = true
|
||||
s.f.headSec.begin(s)
|
||||
s.AddFieldString(FieldNameError,code)
|
||||
s.AddFieldString(FieldNameType ,errType)
|
||||
s.f.headSec.end(s)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (s *state) AddFieldInt (name string, value int ) {
|
||||
s.AddFieldString(name,fmt.Sprintf(s.f.intLayout,value)) }
|
||||
|
||||
func (s *state) AddFieldBool (name string, value bool ) {
|
||||
s.AddFieldString(name,fmt.Sprintf(s.f.boolLayout,value)) }
|
||||
|
||||
func (s *state) AddFieldFloat (name string, value float64 ) {
|
||||
s.AddFieldString(name,fmt.Sprintf(s.f.floatLayout,value)) }
|
||||
|
||||
func (s *state) AddFieldTime (name string, value time.Time) {
|
||||
s.AddFieldString(name,fmt.Sprintf(s.f.timeLayout, value)) }
|
||||
|
||||
func (s *state) AddFieldDate (name string, value time.Time) {
|
||||
s.AddFieldString(name,fmt.Sprintf(s.f.dateLayout,value)) }
|
||||
|
||||
|
||||
func (s *state) AddFieldString (name, value string) {
|
||||
if !s.doFields {}
|
||||
}
|
||||
|
||||
|
||||
func (s *state) AddString(str string) {}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func (s *state) format (err error, level int, isWrapped, addWrap, addCause, addTrace bool) (fe error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
d := false
|
||||
defer func (st *state, dn *bool, er error, rs *error){
|
||||
if !(*dn) {
|
||||
if rc := recover(); fe != nil {
|
||||
ec,is := rc.(error)
|
||||
if !is {
|
||||
ec = fmt.Errorf("%#v",rc)
|
||||
}
|
||||
*rs = *NewErrBuilderFormat(ec,reflect.TypeOf(er),true)
|
||||
}
|
||||
}
|
||||
}(s,&d,err,&fe)
|
||||
|
||||
if fb,is := err.(Buildable); is {
|
||||
fb.Build(s)
|
||||
} else {
|
||||
rt := reflect.TypeOf(err)
|
||||
if rt.Kind() == reflect.Pointer {
|
||||
rt = rt.Elem()
|
||||
}
|
||||
if eb := GetErrorBuilder(rt); eb != nil {
|
||||
eb.BuildError(s,err)
|
||||
} else {
|
||||
eb = MakeErrorBuilder(s,err)
|
||||
SetErrorBuilder(rt,eb)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if addWrap {
|
||||
if uw,is := err.(interface { Unwrap() error }); is {
|
||||
if ne := uw.Unwrap(); ne != nil {
|
||||
if ew := s.format(ne, level,true, addWrap,true, true ); ew != nil {
|
||||
// añadimos un field fmt-(nombre del tipo de error) con el code
|
||||
// o mensaje de error
|
||||
rt := reflect.TypeOf(ne).String()
|
||||
s.AddFieldString(fmt.Sprintf(fmtFieldFormatError,rt),fmt.Sprintf(fmtErrorFormatUnwrap,rt))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MultiErr
|
||||
if ua,is := err.(interface { Unwrap() []error }); is {
|
||||
le := ua.Unwrap()
|
||||
if len(le) > 0 {
|
||||
s.f.multiSec.begin(s)
|
||||
for _, el := range ua.Unwrap() {
|
||||
if el != nil {
|
||||
pos := s.b.Len()
|
||||
if me := s.format(el, level, false, addWrap, true, false); me != nil {
|
||||
// borramos lo incorporado al buffer como precaucion
|
||||
s.b.Truncate(pos)
|
||||
// añadimos un field fmt-(nombre del tipo de error) con el code
|
||||
// o mensaje de error
|
||||
rt := reflect.TypeOf(el).String()
|
||||
s.AddFieldString(fmt.Sprintf(fmtFieldFormatError, rt), fmt.Sprintf(fmtErrorTypeFormat, rt))
|
||||
}
|
||||
}
|
||||
}
|
||||
s.f.multiSec.end(s)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if addCause {
|
||||
if cs, is := err.(interface{ Cause() error }); is {
|
||||
if ne := cs.Cause(); ne != nil {
|
||||
pos:= s.b.Len()
|
||||
s.f.causeSec.begin(s)
|
||||
if ec := s.format(ne, level+1, false, addWrap, addCause, addTrace); ec != nil {
|
||||
// borramos lo incorporado al buffer como precaucion
|
||||
s.b.Truncate(pos)
|
||||
// añadimos un field fmt-(nombre del tipo de error) con el code
|
||||
// o mensaje de error
|
||||
rt := reflect.TypeOf(ne).String()
|
||||
s.AddFieldString(fmt.Sprintf(fmtFieldFormatError, rt), fmt.Sprintf(fmtErrorTypeFormat, rt))
|
||||
}
|
||||
s.f.causeSec.end(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if addTrace {
|
||||
if tc, is := err.(interface{ stack.Tracer }); is {
|
||||
if st := tc.StackTrace(); st != nil {
|
||||
s.doTrace = false
|
||||
s.f.traceSec.begin(s)
|
||||
for n,f := range st.Frames() {
|
||||
s.f.fmeBuilder.BuildFrame(s,f,n)
|
||||
}
|
||||
s.f.traceSec.end(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in new issue