creando el formatter de errores de nuevo para hacerlo mas flexible

master
DEV 3 years ago
parent e17f3995fa
commit 15852b0712

@ -2,7 +2,6 @@ package main
import (
"bytes"
"errs"
"fmt"
)
@ -35,6 +34,8 @@ func (m MiCustomErr) Unwrap () error {
func main() {
//var MC MiCustomErr
//errs.G.Formatter.(*errs.FmtText).SetErrorFormatter(reflect.TypeOf(MC),fm)
@ -44,7 +45,7 @@ func main() {
eC := eA.ToErrCause(
eB.ToErrInfo(errs.ErrInfoWithCause(errs.ErrCode("--ARRROR---").ToErrInfo(errs.ErrInfoWithMessage("Mi Mensage"),
errs.ErrInfoWithStackTrace(3)))))
eM := errs.MakeErrGroup(MiCustomErr{
eM := errs.MakeErrMulti(MiCustomErr{
error: errs.NewErrInfo(eC,
errs.ErrInfoWithMessage("Esto es el mensaje"),
errs.ErrInfoWithInfo ("Informacion del error"),
@ -58,4 +59,27 @@ func main() {
fmt.Print(string(errs.G.Formatter.Format(eM)))
/*
ercode := errs.ErrCode("err-code")
fmt.Print(ercode.String())
fmt.Print("\n")
erpanic := errs.NewErrPanic("err-mensaje-panico")
fmt.Print(erpanic.String())
fmt.Print("\n")
ercause := ercode.ToErrCause(errs.ErrCode("err-code-with-cause"))
fmt.Print(ercause.String())
fmt.Print("\n")
erInfo := errs.ErrCode("err-code-with-info").ToErrInfo(errs.ErrInfoWithMessage("Esto es un mensaje"))
fmt.Print(erInfo.String())
fmt.Print("\n")
ertrace := errs.ErrCode("err-code-with-trace").ToErrTrace(0)
fmt.Print(ertrace.String())
fmt.Print("\n")
*/
}

@ -23,10 +23,6 @@
package bins
import "errs"
const (
minBitSize = 6

@ -4,7 +4,7 @@
//
// File name err_cause.go
// Created by DEV
// Modified 23/03/2024
// Modified 30/03/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
@ -23,6 +23,8 @@
package errs
type (
ErrCause struct {
@ -32,44 +34,17 @@ type (
)
func NewErrCause (err error, cause error) *ErrCause {
if err == nil || cause == nil {
return nil
}
return &ErrCause{
error: err,
cause: cause,
}
}
func (e ErrCause) Unwrap() error {
return e.error }
func (e ErrCause) Cause () error {
return e.cause }
func (e ErrCause) Unwrap() error {
return e.error }
func (e ErrCause) StackTrace () StackTrace {
if st,is := e.error.(StackTracer); is {
return st.StackTrace()
}
if st,is := e.cause.(StackTracer); is { return st.StackTrace() }
return nil
func (e ErrCause) Format (builder FormatBuilder) {
builder.AddFieldString("error",e.Error())
builder.AddType("core.errs.ErrorCause")
builder.AddCause(e.cause)
}
func (e ErrCause) ToErrInfo(options ...ErrInfoFnc) ErrInfo {
return *NewErrInfo(e,options...) }
func (e ErrCause) ToErrTrace(offset int) ErrTrace {
return *NewErrTrace(e.error,offset) }
func (e ErrCause) String () string {
return string(G.Formatter.Format(e)) }

@ -4,7 +4,7 @@
//
// File name err_code.go
// Created by DEV
// Modified 23/03/2024
// Modified 30/03/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
@ -23,6 +23,10 @@
package errs
type (
ErrCode string
@ -31,54 +35,25 @@ type (
func (e ErrCode) Error () string {
return string(e) }
func (e ErrCode) Code () string {
func (e ErrCode) Error() string {
return string(e) }
func (e ErrCode) Code () string {
return string(e) }
func (e ErrCode) Unwrap() error {
func (_ ErrCode) Unwrap() error {
return nil }
func (_ ErrCode) UnwrapAll() []error {
return nil }
func (e ErrCode) Is(target error) bool {
if te,is := target.(interface { Code() string}); is {
return te.Code() == e.Code()
}
return target.Error() == string(e)
}
func (e ErrCode) As(target any) bool {
if target == nil {
return false
}
switch t := target.(type) {
case *ErrCode:
*t = e
return true
case *ErrCause:
return e.As(target.(*ErrCause).cause)
default:
return false
}
}
func (e ErrCode) ToErrCause(err error) ErrCause {
return *NewErrCause(e,err) }
func (e ErrCode) ToErrInfo(options ...ErrInfoFnc) ErrInfo {
return *NewErrInfo(e,options...) }
func (e ErrCode) ToErrTrace(offset int) ErrTrace {
return *NewErrTrace(e,offset) }
func (e ErrCode) Format (builder FormatBuilder) {
builder.AddFieldString("error",string(e))
builder.AddType("core.errs.ErrorCode")
}
func (e ErrCode) String () string {
return string(G.Formatter.Format(e)) }

@ -0,0 +1,57 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name fomat_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 "time"
type (
FormatBuilder interface {
Options() FormatOps
Reset ()
Level () int
Error () error
Bytes () []byte
AddError (err error, options ...FormatOpsFnc)
AddCause (err error, options ...FormatOpsFnc)
AddType (ter string, options ...FormatOpsFnc)
//AddTrace (trace StackTrace, options ...FormatOpsFnc)
AddString (str string, options ...FormatOpsFnc)
AddStringLn (str string, options ...FormatOpsFnc)
AddFieldString (name string, value string , options ...FormatOpsFnc)
AddFieldInt (name string, value int , options ...FormatOpsFnc)
AddFieldFloat (name string, value float64 , options ...FormatOpsFnc)
AddFieldTime (name string, value time.Time, options ...FormatOpsFnc)
}
)

@ -27,34 +27,34 @@ import "fmt"
const (
FmtTextColorBlack FmtTextColor = iota + 30
FmtTextColorRed
FmtTextColorGreen
FmtTextColorYellow
FmtTextColorBlue
FmtTextColorMagenta
FmtTextColorCyan
FmtTextColorWhite
FormatColorBlack FormatColor = iota + 30
FormatColorRed
FormatColorGreen
FormatColorYellow
FormatColorBlue
FormatColorMagenta
FormatColorCyan
FormatColorWhite
)
type (
FmtTextColor uint8
FormatColor uint8
)
func (c FmtTextColor) BeginString () string {
func (c FormatColor) BeginColorString() string {
return fmt.Sprintf("\u001B[%dm",uint8(c))
}
func (_ FmtTextColor) EndString () string {
func (_ FormatColor) EndColorString () string {
return "\x1b[0m" }
func (c FmtTextColor) Sprintf (fm string, params ...any) string {
func (c FormatColor) 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 >= FmtTextColorBlack && c <= FmtTextColorWhite }
func (c FormatColor) IsValid() bool {
return c >= FormatColorBlack && c <= FormatColorWhite }

@ -0,0 +1,54 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name error_formattable_options.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
type (
FormatOps struct {
FormatShow
Indent string
Color FormatColor
FieldNameColor FormatColor
FieldValueColor FormatColor
FieldNameAdapter func(string) string
}
FormatOpsFnc func (ops *FormatOps)
)
func (o *FormatOps) Clone (options ...FormatOpsFnc) *FormatOps {
if len(options) == 0 {
return o
}
no := *o
for _,op := range options {
op(&no)
}
return &no
}

@ -0,0 +1,67 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name format_use_ops.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
const (
FormatShowType FormatShow = 1 << iota
FormatShowColors
FormatShowIndent
FormatShowDetails
FormatShowCause
FormatShowTrace
FormatShowFieldEmpty
)
type (
FormatShow uint8
)
func (u FormatShow) ShowType () bool {
return u&FormatShowType !=0 }
func (u FormatShow) ShowColors () bool {
return u&FormatShowColors !=0 }
func (u FormatShow) ShowIndent () bool {
return u&FormatShowIndent !=0 }
func (u FormatShow) ShowDetails() bool {
return u&FormatShowDetails !=0 }
func (u FormatShow) ShowCause () bool {
return u&FormatShowCause !=0 }
func (u FormatShow) ShowTrace () bool {
return u&FormatShowTrace !=0 }
func (u FormatShow) ShowFieldEmpty () bool {
return u&FormatShowFieldEmpty !=0 }

@ -0,0 +1,44 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name formattable.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
type (
Formattable interface {
Build (builder FormatBuilder)
}
FormattableFnc func(builder FormatBuilder)
)
func (f FormattableFnc) Build (builder FormatBuilder) {
f(builder) }

@ -4,7 +4,7 @@
//
// File name formatter.go
// Created by DEV
// Modified 25/03/2024
// Modified 30/03/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
@ -23,19 +23,21 @@
package errs
type (
Formatter interface {
Formatter interface {
Format (err error) []byte
}
FormatFnc func (err error) []byte
FormatterFnc func(err error) []byte
)
func (f FormatFnc) Format (err error) []byte {
func (f FormatterFnc) Format (err error) []byte {
return f(err) }

@ -59,6 +59,7 @@ var (
errInfoRT reflect.Type
errPanicRT reflect.Type
errGroupRT reflect.Type
errMultiRT reflect.Type
stackTraceRType reflect.Type
}{
@ -79,6 +80,7 @@ var (
errPanicRT : reflect.TypeOf(ErrPanic{}),
errInfoRT : reflect.TypeOf(ErrInfo {}),
errGroupRT : reflect.TypeOf(ErrGroup{}),
errMultiRT : reflect.TypeOf(ErrMulti(nil)),
}
)
@ -123,6 +125,9 @@ func UnwrapAll(err error) ErrList {
if err == nil {
return nil
}
if al,is := err.(AllUnwrapper); is {
return al.UnwrapAll()
}
var l ErrList
e := err
for e!= nil {

@ -0,0 +1,75 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name err_cause.go
// Created by DEV
// Modified 23/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
type (
ErrCause struct {
error
cause error
}
)
func NewErrCause (err error, cause error) *ErrCause {
if err == nil || cause == nil {
return nil
}
return &ErrCause{
error: err,
cause: cause,
}
}
func (e ErrCause) Cause () error {
return e.cause }
func (e ErrCause) Unwrap() error {
return e.error }
func (e ErrCause) StackTrace () StackTrace {
if st,is := e.error.(StackTracer); is {
return st.StackTrace()
}
if st,is := e.cause.(StackTracer); is { return st.StackTrace() }
return nil
}
func (e ErrCause) ToErrInfo(options ...ErrInfoFnc) ErrInfo {
return *NewErrInfo(e,options...) }
func (e ErrCause) ToErrTrace(offset int) ErrTrace {
return *NewErrTrace(e.error,offset) }
func (e ErrCause) String () string {
return string(G.Formatter.Format(e)) }

@ -0,0 +1,64 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name err_code.go
// Created by DEV
// Modified 23/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
type (
ErrCode string
)
func (e ErrCode) Error () string {
return string(e) }
func (e ErrCode) Code () string {
return string(e) }
func (e ErrCode) Unwrap() error {
return nil }
func (e ErrCode) ToErrCause(err error) ErrCause {
return *NewErrCause(e,err) }
func (e ErrCode) ToErrInfo(options ...ErrInfoFnc) ErrInfo {
return *NewErrInfo(e,options...) }
func (e ErrCode) ToErrTrace(offset int) ErrTrace {
return *NewErrTrace(e,offset) }
func (e ErrCode) String () string {
return string(G.Formatter.Format(e)) }

@ -61,6 +61,7 @@ func (l ErrGroup) Map (mp func(error) bool) {
}
}
func (l ErrGroup) Unwrap() error {
if len(l) > 0 {
return l[0]
@ -68,6 +69,11 @@ func (l ErrGroup) Unwrap() error {
return nil
}
/*
func (l ErrGroup) UnwrapAll () []error {
return l }
*/
func (l ErrGroup) ToErrInfo(options ...ErrInfoFnc) ErrInfo {

@ -96,5 +96,8 @@ func (l ErrList) filter(filter func(error) bool) ErrList {
rs = append(rs, e)
}
}
if len(rs) == 0 {
return nil
}
return rs
}

@ -0,0 +1,76 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name err_multi.go
// Created by DEV
// Modified 29/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
type (
ErrMulti ErrList
)
func MakeErrMulti (err ...error) ErrMulti {
return ErrMulti(ErrList(err).Clean()) }
func (e ErrMulti) Error () string {
return "multi-errors" }
func (e ErrMulti) Map (mp func(error) bool) {
for _,i := range e {
if !mp(i) {
return
}
}
}
func (e ErrMulti) Unwrap() error {
if len(e) > 0 {
return nil
}
return e[0]
}
func (e ErrMulti) ToErrInfo(options ...ErrInfoFnc) ErrInfo {
return *NewErrInfo(e,options ...) }
func (e ErrMulti) ToErrTrace(offset int) ErrTrace {
return *NewErrTrace(e,offset) }
func (e ErrMulti) String () string {
return string(G.Formatter.Format(e)) }

@ -62,3 +62,7 @@ func (e ErrTrace) StackTrace () StackTrace {
func (e ErrTrace) ToErrMetas (options ...ErrInfoFnc) *ErrInfo {
return NewErrInfo(e,options...) }
func (e ErrTrace) String () string {
return string(G.Formatter.Format(e)) }

@ -0,0 +1,40 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name err_formatter.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
type (
ErrorFormatter interface {
Format (err error) []byte
}
FormatErrorFnc func (err error)
)
func (f FormatErrorFnc) Format (err error) {
f(err) }

@ -40,11 +40,17 @@ type (
FmtText struct {
FmtTextAtt
dateLayout string
fixName int
fieldSep string
indent string
dateLayout string
fmtField string
errorHead func(s *FmtTextState, e error)
prevHead func(s *FmtTextState, e error)
traceHead func(s *FmtTextState, e error)
traceFmt func(s *FmtTextState, t StackTrace)
providers sync.Map
}
@ -57,7 +63,7 @@ type (
func NewFmtText(options ...FmtTextFnc) *FmtText {
fm := applyFmtTextOptions(&FmtText{
FmtTextAtt: FmtTextAttIndent | FmtTextAttVerbose | FmtTextAttTrace | FmtTextAttColors | FmtTextAttEmpty,
FmtTextAtt: FmtTextAttIndent | FmtTextAttDetails | FmtTextAttTrace | FmtTextAttColors | FmtTextAttEmpty,
dateLayout: time.RFC822Z,
indent : " ",
fixName : 12,
@ -75,7 +81,7 @@ func NewFmtText(options ...FmtTextFnc) *FmtText {
func (f *FmtText) Format (err error) []byte {
func (f *FmtText) Format (err error) (res []byte) {
if err == nil {
return nil
}
@ -83,20 +89,22 @@ func (f *FmtText) Format (err error) []byte {
dn := false
defer func(d *bool, s *FmtTextState) {
s.Buffer.Reset()
if !(*d) {
e := NewErrPanic(recover())
if e != nil {
s.WriteFieldString("panic",e.Error() )
if e := recover(); e != nil {
panic(e)
f.format(s,NewErrPanic(e).ToTrace(1),true)
res = s.Buffer.Bytes()
}
}
s.Reset()
}(&dn,st)
f.formatAll(st,err)
f.format(st,err,true)
if f.AddStackTrace() {
if tc := UnwrapStackTrace(err); st != nil {
if tc := UnwrapStackTrace(err); tc != nil {
st.WriteString("\nStack Trace\n")
f.traceFmt(st, tc.StackTrace())
}
@ -108,46 +116,42 @@ func (f *FmtText) Format (err error) []byte {
}
func (f *FmtText) formatAll (s *FmtTextState, err error) {
func (f *FmtText) format (s *FmtTextState, err error, head bool) {
rt := reflect.TypeOf(err)
if rt.Kind() == reflect.Pointer {
err = reflect.ValueOf(err).Elem().Interface().(error)
if err == nil {
return
}
rt = rt.Elem()
}
s.WriteFieldString("error",err.Error(),FmtTextColorRed)
s.WriteFieldString("error-type",reflect.TypeOf(err).String())
if head {
s.WriteFieldString("error", err.Error(), FormatColorRed)
s.WriteFieldString("error-type", rt.String())
}
f.getFmtTextProvider(rt).Format(s,err)
ls := UnwrapAll(err)
for _,e := range ls {
f.format(s,e)
ue := Unwrap(err)
for ue != nil {
f.format(s,ue,false)
ue = Unwrap(ue)
}
/*
if c := UnwrapCause(err); c != nil {
s.writeString("-Previous-----------------------------------------\n",[]FmtTextColor{FmtTextColorCyan})
s.writeString("-Previous-----------------------------------------\n",[]FormatColor{FormatColorCyan})
s.IncLevel()
if f.Verbose() {
f.formatAll(s, c.Cause())
} else {
f.format(s,c.Cause())
}
f.format(s,c.Cause(),true)
s.DecLevel()
}
*/
}
func (f *FmtText) format (s *FmtTextState, err error) {
rt := reflect.TypeOf(err)
//s.WriteFieldString("error",err.Error())
//s.WriteFieldString("error-type",rt.String())
if rt.Kind() == reflect.Pointer {
rt = rt.Elem()
err = reflect.ValueOf(err).Elem().Interface().(error)
if err == nil {
return
}
}
f.getFmtTextProvider(rt).Format(s,err)
}
func (f *FmtText) getFmtTextProvider(rt reflect.Type) FmtTextProvider {
@ -194,6 +198,7 @@ func (f *FmtText) buildFmtTextProvider (rtype reflect.Type) FmtTextProvider {
case G.errPanicRT: return FmtTextProviderFnc(formatErrPanic)
case G.errInfoRT : return FmtTextProviderFnc(formatErrInfo )
case G.errGroupRT: return FmtTextProviderFnc(formatErrGroup)
case G.errMultiRT: return FmtTextProviderFnc(formatErrMulti)
default : return FmtTextProviderFnc(formatErrAny)
}
}
@ -211,11 +216,12 @@ func formatErrPanic (s *FmtTextState, e error ){
}
func formatErrTrace (s *FmtTextState, e error) {
s.WriteTrace (e.(ErrTrace).StackTrace())
//s.WriteFieldString("error-Trace",e.(ErrTrace).Error())
}
func formatErrCause (s *FmtTextState, e error) {
//s.WriteFieldString("error-cause",e.(ErrCause).Error())
s.WriteCause(e.(ErrCause).Cause())
}
func formatErrGroup (s *FmtTextState, e error) {
@ -225,19 +231,31 @@ func formatErrGroup (s *FmtTextState, e error) {
s.Buffer.WriteString("** Error Group (")
s.Buffer.WriteString(strconv.Itoa(i))
s.Buffer.WriteString(") ********************\n")
s.fmt.format(s,err)
s.fmt.format(s,err,true)
return true
})
}
func formatErrMulti (s *FmtTextState, e error) {
i := 0
e.(ErrMulti).Map(func(err error) bool {
i++
s.Buffer.WriteString("** Multiple errors (")
s.Buffer.WriteString(strconv.Itoa(i))
s.Buffer.WriteString(") ********************\n")
s.fmt.format(s,err,true)
return true
})
}
func formatErrInfo (s *FmtTextState, e error) {
ei := e.(ErrInfo)
s.WriteFieldString("message" ,ei.Message())
s.WriteFieldString("info" ,ei.Info ())
if s.Level() == 0 {
s.WriteFieldTime("date-time",ei.Date(),FmtTextColorRed,FmtTextColorMagenta)
s.WriteFieldTime("date-time",ei.Date(),FmtTextColorGreen)
s.WriteFieldTime("date-time",ei.Date())
s.WriteFieldTime("date-time",ei.Date(), FormatColorRed, FormatColorMagenta)
ei.MapMetas(func(n string, v any) bool {
s.WriteFieldString(n,fmt.Sprintf("%v",v))
return true
@ -251,25 +269,31 @@ func formatErrAny (s *FmtTextState, err error) {
func formatStackTrace(s *FmtTextState, t StackTrace) {
for _,f := range t.Frames() {
s.WriteString(f.Package+"."+f.FuncName,FmtTextColorYellow)
s.WriteString(fmt.Sprintf(" %.90s:%d",f.File,f.LineNumber),FmtTextColorBlue)
s.WriteString(f.Package+"."+f.FuncName, FormatColorYellow)
s.WriteString(fmt.Sprintf(" %.90s:%d",f.File,f.LineNumber), FormatColorBlue)
if sc,er := f.SourceLine(); er == nil {
if len(sc) > 80 {
ls := typex.SplitString(sc,70)
for _,l := range ls {
s.Buffer.WriteString(FmtTextColorMagenta.BeginString())
s.Buffer.WriteString(FormatColorMagenta.BeginColorString())
s.Buffer.WriteString(" ")
s.Buffer.WriteString(l)
s.Buffer.WriteByte('\n')
s.Buffer.WriteString(FmtTextColorMagenta.EndString())
s.Buffer.WriteString(FormatColorMagenta.EndColorString())
}
continue
}
s.Buffer.WriteString(" ")
s.Buffer.WriteString(FmtTextColorMagenta.Sprintf(sc))
s.Buffer.WriteString(FormatColorMagenta.Sprintf(sc))
s.Buffer.WriteByte('\n')
}
}
}
func errorHead (s *FmtTextState, e error) {
s.WriteFieldString("error", e.Error(), FormatColorRed)
s.WriteFieldString("error-type", reflect.TypeOf(e).String())
}

@ -26,7 +26,7 @@ package errs
const(
FmtTextAttVerbose FmtTextAtt = 1 << iota
FmtTextAttDetails FmtTextAtt = 1 << iota
FmtTextAttEmpty
FmtTextAttIndent
FmtTextAttTrace
@ -43,17 +43,17 @@ type (
func (a FmtTextAtt) Verbose () bool {
return a&FmtTextAttVerbose != 0 }
func (a FmtTextAtt) AddDetails() bool {
return a&FmtTextAttDetails != 0 }
func (a FmtTextAtt) IgnoreEmpties () bool {
func (a FmtTextAtt) AddEmpties() bool {
return a&FmtTextAttEmpty != 0 }
func (a FmtTextAtt) UseIndent () bool {
func (a FmtTextAtt) AddIndent() bool {
return a&FmtTextAttIndent != 0 }
func (a FmtTextAtt) AddStackTrace () bool {
return a&FmtTextAttTrace != 0 }
func (a FmtTextAtt) UseColors () bool {
func (a FmtTextAtt) AddColors() bool {
return a&FmtTextAttColors != 0 }

@ -39,6 +39,9 @@ type (
Error error
Buffer bytes.Buffer
}
FmtTextStateFnc func(s *FmtTextState, err error)
)
@ -50,7 +53,7 @@ func newFmtTextState(fmt *FmtText) *FmtTextState {
func (s *FmtTextState) buildIndent() string {
if !s.fmt.UseIndent() {
if !s.fmt.AddIndent() {
return ""
}
if s.level == 0 {
@ -68,24 +71,55 @@ func (s *FmtTextState) IncLevel() {
func (s *FmtTextState) DecLevel() {
s.level-- }
func (s *FmtTextState) Reset () {
s.Buffer.Reset()
s.level = 0
s.fmt = nil
}
func (s *FmtTextState) WriteString (str string, color ...FmtTextColor) {
s.writeString(fmt.Sprintf("%s%s\n",s.buildIndent(),str),color) }
func (s *FmtTextState) WriteCause (err error) {
if err == nil {
return
}
s.level++
defer func(st *FmtTextState) {
st.level--
}(s)
s.fmt.format(s,err,true)
}
func (s *FmtTextState) WriteFieldString (name, value string, color ...FmtTextColor) {
if s.fmt.IgnoreEmpties() && len(value) == 0 {
func (s *FmtTextState) WriteTrace (trace StackTrace) {
if trace == nil {
return
}
if s.level == 0 && s.fmt.AddStackTrace() {
s.fmt.traceFmt(s, trace)
}
}
func (s *FmtTextState) WriteError (err error, head bool) {
s.fmt.format(s,err,head) }
func (s *FmtTextState) WriteString (str string, color ...FormatColor) {
s.writeString(fmt.Sprintf("%s%s\n",s.buildIndent(),str),color) }
func (s *FmtTextState) WriteFieldString (name, value string, color ...FormatColor) {
if s.fmt.AddEmpties() && len(value) == 0 {
return
}
s.writeField(name,value,color)
}
func (s *FmtTextState) WriteFieldInt (name string, num int, color ...FmtTextColor) {
func (s *FmtTextState) WriteFieldInt (name string, num int, color ...FormatColor) {
s.writeField(name,strconv.Itoa(num),color) }
func (s *FmtTextState) WriteFieldFloat(name string, num float64, dec int, color ...FmtTextColor) {
func (s *FmtTextState) WriteFieldFloat(name string, num float64, dec int, color ...FormatColor) {
f := "%-10s: %f\n"
if dec < 0 {
f = "%-10s: %." + strconv.Itoa(dec) + "f\n"
@ -93,11 +127,12 @@ func (s *FmtTextState) WriteFieldFloat(name string, num float64, dec int, color
s.writeField(name,fmt.Sprintf(f,num),color)
}
func (s *FmtTextState) WriteFieldTime (name string, date time.Time, color ...FmtTextColor) {
func (s *FmtTextState) WriteFieldTime (name string, date time.Time, color ...FormatColor) {
s.writeField(name,date.Format(s.fmt.dateLayout),color) }
func (s *FmtTextState) writeField (name, value string, color []FmtTextColor) {
func (s *FmtTextState) writeField (name, value string, color []FormatColor) {
s.Buffer.WriteString(s.buildIndent())
if len(color) == 0 || !color[0].IsValid() {
s.Buffer.WriteString(fmt.Sprintf(s.fmt.fmtField, name))
@ -108,31 +143,31 @@ func (s *FmtTextState) writeField (name, value string, color []FmtTextColor) {
}
c1 := color[0]
s.Buffer.WriteString(c1.BeginString())
s.Buffer.WriteString(c1.BeginColorString())
s.Buffer.WriteString(fmt.Sprintf(s.fmt.fmtField,name))
if len(color) == 1 || !color[1].IsValid() {
s.Buffer.WriteString(s.fmt.fieldSep)
s.Buffer.WriteString(value)
s.Buffer.WriteString(c1.EndString())
s.Buffer.WriteString(c1.EndColorString())
} else {
s.Buffer.WriteString(c1.EndString())
s.Buffer.WriteString(c1.EndColorString())
s.Buffer.WriteString(s.fmt.fieldSep)
s.Buffer.WriteString(color[1].BeginString())
s.Buffer.WriteString(color[1].BeginColorString())
s.Buffer.WriteString(fmt.Sprintf(s.fmt.fmtField,value))
s.Buffer.WriteString(color[1].EndString())
s.Buffer.WriteString(color[1].EndColorString())
}
s.Buffer.WriteString("\n")
}
func (s *FmtTextState) writeString (str string, color []FmtTextColor) {
func (s *FmtTextState) writeString (str string, color []FormatColor) {
if len(color) == 0 || !color[0].IsValid() {
s.Buffer.WriteString(str)
return
}
s.Buffer.WriteString(color[0].BeginString())
s.Buffer.WriteString(color[0].BeginColorString())
s.Buffer.WriteString(str)
s.Buffer.WriteString(color[0].EndString())
s.Buffer.WriteString(color[0].EndColorString())
}

@ -0,0 +1,53 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name error_format_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 "time"
type (
FormatBuilder interface {
Reset ()
Level () int
Error () error
Bytes () []byte
AddError (err error, options ...FormatOpsFnc)
AddCause (err error, options ...FormatOpsFnc)
AddTrace (trace StackTrace, options ...FormatOpsFnc)
AddString (str string, options ...FormatOpsFnc)
AddStringLn (str string, options ...FormatOpsFnc)
AddFieldString (name string, value string , options ...FormatOpsFnc)
AddFieldInt (name string, value int , options ...FormatOpsFnc)
AddFieldFloat (name string, value float64 , options ...FormatOpsFnc)
AddFieldTime (name string, value time.Time, options ...FormatOpsFnc)
}
)

@ -0,0 +1,60 @@
// ------------------------------------------------------------------------
// 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 errs
import "fmt"
const (
FormatColorBlack FormatColor = iota + 30
FormatColorRed
FormatColorGreen
FormatColorYellow
FormatColorBlue
FormatColorMagenta
FormatColorCyan
FormatColorWhite
)
type (
FormatColor uint8
)
func (c FormatColor) BeginColorString() string {
return fmt.Sprintf("\u001B[%dm",uint8(c))
}
func (_ FormatColor) EndColorString () string {
return "\x1b[0m" }
func (c FormatColor) Sprintf(fm string, params ...any) string {
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c),fmt.Sprintf(fm,params...)) }
func (c FormatColor) IsValid() bool {
return c >= FormatColorBlack && c <= FormatColorWhite }

@ -0,0 +1,37 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name error_formattable_options.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
type (
FormatOps struct {
FieldNameColor FormatColor
FieldValueColor FormatColor
}
FormatOpsFnc func (ops *FormatOps)
)

@ -0,0 +1,40 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name error_formatable.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
type (
Formattable interface {
Build (builder FormatBuilder)
}
FormattableFnc func(builder FormatBuilder)
)
func (f FormattableFnc) Build (builder FormatBuilder) {
f(builder) }

@ -0,0 +1,41 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name formatter.go
// Created by DEV
// Modified 25/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
type (
Formatter interface {
Format (err error) []byte
}
FormatFnc func (err error) []byte
)
func (f FormatFnc) Format (err error) []byte {
return f(err) }

@ -0,0 +1,3 @@
module errs
go 1.22

@ -26,12 +26,21 @@ package errs
type (
Unwrapper interface {
Unwrapper interface {
Unwrap () error
}
UnwrapperFnc func () error
AllUnwrapper interface {
UnwrapAll () []error
}
UnwrapperAllFnc func () []error
)
@ -40,3 +49,6 @@ func (f UnwrapperFnc) Unwrap () error {
func (f UnwrapperAllFnc) UnwrapAll () []error {
return f() }

@ -23,10 +23,6 @@
package tasks
import "errs"
const (
CompletionFromChild int = 0

@ -23,13 +23,6 @@
package timers
import (
"errs"
)
var (

Loading…
Cancel
Save

Powered by TurnKey Linux.