incluidas las librerias de transformacion de strings

master
DEV 3 years ago
parent d96d15cb9b
commit c23d60ba22

@ -2,11 +2,12 @@ package main
import (
"errs"
"errs/stack"
"fmt"
)
func printTrace (s errs.StackTrace) {
func printTrace (s stack.Trace) {
lf := s.Frames()
for i,f := range lf {
fmt.Printf("%d file %s line %d %s\n",i,f.File,f.LineNumber,f.FuncName)
@ -44,25 +45,31 @@ func (m MiCustomErr) Unwrap () error {
func main() {
ercode := errs.ErrCode("error->err-code")
fmt.Print(ercode.String())
fmt.Print(errs.ToFormat(ercode,nil))
fmt.Print("\n")
erpanic := errs.NewErrPanic("error->err-mensaje-panico")
fmt.Print(erpanic.String())
fmt.Print(errs.ToFormat(erpanic,nil))
fmt.Print("\n")
ercause := ercode.ToErrCause(errs.ErrCode("error->err-code-with-cause"))
fmt.Print(ercause.String())
panicInfo := erpanic.WrapWithInfo(errs.ErrInfoWithMessage("Un mensaje de panico"))
fmt.Print(errs.ToFormat(panicInfo,nil))
fmt.Print("\n")
erInfo := errs.ErrCode("err-code-with-info").ToErrInfo(errs.ErrInfoWithMessage("Esto es un mensaje"))
fmt.Print(erInfo.String())
ercause := ercode.WrapWithCause(errs.ErrCode("causa-2").WrapWithCause(errs.ErrCode("causa-1")))
fmt.Print(errs.ToFormat(ercause,nil))
fmt.Print("\n")
erInfo := errs.ErrCode("err-code-with-info").WrapWithInfo(errs.ErrInfoWithMessage("Esto es un mensaje"))
fmt.Print(errs.ToFormat(erInfo,nil))
fmt.Print("\n")
fmt.Print(string(errs.G.Formatter.Format(&MiCustomErr{error:errs.ErrCode("UnErrorCU") })))
fmt.Print("\n")
/*
ertrace := errs.ErrCode("err-code-with-trace").ToErrTrace(0)
fmt.Print(ertrace.String())

@ -26,58 +26,45 @@ package errs
import (
"atypes/astrings"
"errs/fmts"
"errs/stack"
"fmt"
"reflect"
"sync"
)
const (
stackSkipFrames = 2
stackMaxFrames = 32
stackUnknownInfo = "???"
)
var (
G = struct {
mux sync.RWMutex
Formatter Formatter
extractors FormatExtractors
Formatter fmts.Formatter
ErrGroup ErrCode
ErrPanic ErrCode
ErrParamNil ErrCode
ErrParamInvalid ErrCode
ErrParamOutOfRange ErrCode
ErrFormat ErrCode
ErrFormatLevel ErrCode
stackTraceRType reflect.Type
}{
extractors : DefaultsExtractors(),
ErrGroup : ErrCode("group-error"),
ErrPanic : ErrCode("panic-error"),
ErrParamNil : ErrCode("param-nil"),
ErrParamInvalid : ErrCode("param-invalid"),
ErrParamOutOfRange : ErrCode("param-out-of-range"),
ErrFormat : ErrCode("format-error"),
ErrFormatLevel : ErrCode("format-invalid-level"),
stackTraceRType : reflect.TypeOf(StackTrace(nil)),
stackTraceRType : reflect.TypeOf(stack.Trace(nil)),
Formatter : NewFmtText(func (f *FmtText) {
Formatter : fmts.NewFmtText(func (f *fmts.FmtText) {
f.FixFieldLen = 13
f.StringOnCause = "Cause ------------------------\n"
f.StringOnError = "Error ------------------------\n"
},
FmtTextWithNameTransformer(
fmts.FmtTextWithNameTransformer(
astrings.ToKebab,
)),
@ -86,6 +73,8 @@ var (
func ToErr (err any) error {
if err == nil {
return nil
@ -98,82 +87,28 @@ func ToErr (err any) error {
}
func Wrap (err, cause error) error {
if err == nil {
return cause
func ToFormat (err error, formatter fmts.Formatter) string {
if formatter != nil {
return string(formatter.Format(err))
}
if cause == nil {
return err
}
return *NewErrCause(err, cause)
return string(G.Formatter.Format(err))
}
func Unwrap(err error) error {
func ToString (err error) string {
if err == nil {
return nil
return ""
}
if cs,is := err.(Unwrapper); is {
return cs.Unwrap()
if st,is := err.(interface { String() string }); is {
return st.String()
}
return nil
return err.Error()
}
func UnwrapAll(err error) ErrorList {
if err == nil {
return nil
}
if al,is := err.(AllUnwrapper); is {
return al.UnwrapAll()
}
var l ErrorList
e := err
for e!= nil {
l = append(l,e)
e = Unwrap(e)
}
return l
}
func UnwrapCause(err error) error {
e := Unwrap(err)
for e != nil {
cs,is := e.(Causer)
if is {
return cs.Cause()
}
e = Unwrap(e)
}
return nil
}
func UnwrapTrace(err error) StackTrace {
if err == nil {
return nil
}
e := err
for e != nil {
if st,is := e.(StackTracer); is {
return st.StackTrace()
}
e = Unwrap(e)
}
return nil
}
func AddExtractors (ext ... FormatExtractor) {
G.mux.Lock()
G.extractors = append(G.extractors,ext...)
G.mux.Unlock()
}
func Extract (builder FormatBuilder, err error) {
G.mux.RLock()
G.extractors.Extract(builder,err)
G.mux.RUnlock()
}

@ -23,7 +23,7 @@
package errs
import "errs/fmts"
type (
@ -44,8 +44,6 @@ func NewErrCause (err error, cause error) *ErrCause {
}
func (e ErrCause) Unwrap() error {
return e.error }
@ -53,17 +51,24 @@ func (e ErrCause) Cause () error {
return e.cause }
func (e ErrCause) WrapWithInfo (options ...ErrInfoFnc) error {
return WrapWithInfo(e,options...) }
func (e ErrCause) WrapWithTrace(offset int) error {
return WrapWithTrace(e,offset) }
func (e ErrCause) String () string {
return string(G.Formatter.Format(e)) }
func (e ErrCause) BuildFormat (builder FormatBuilder) {
func (e ErrCause) BuildFormat (builder fmts.FormatBuilder) {
builder.AddCode(e.Error())
builder.AddType("core.errs.ErrCause")
builder.AddWrapped(e.error)
builder.AddCause(e.cause)
}

@ -23,9 +23,7 @@
package errs
import "errs/fmts"
type (
@ -42,6 +40,8 @@ func (e ErrCode) Error() string {
func (e ErrCode) Code () string {
return string(e) }
func (_ ErrCode) Unwrap() error {
return nil }
@ -49,19 +49,27 @@ func (_ ErrCode) UnwrapAll() []error {
return nil }
func (e ErrCode) WrapWithInfo (options ...ErrInfoFnc) error {
return WrapWithInfo(e,options...) }
func (e ErrCode) WrapWithCause(cause error) error {
return WrapWithCause(e,cause) }
func (e ErrCode) WrapWithTrace(offset int) error {
return WrapWithTrace(e,offset) }
func (e ErrCode) String () string {
return string(G.Formatter.Format(e)) }
func (e ErrCode) BuildFormat (builder FormatBuilder) {
func (e ErrCode) BuildFormat (builder fmts.FormatBuilder) {
builder.AddCode(string(e))
builder.AddType("core.errs.ErrCode")
}
func (e ErrCode) ToErrCause (cause error) ErrCause {
return *NewErrCause(e,cause) }
func (e ErrCode) ToErrInfo (options ...ErrInfoFnc) ErrInfo {
return *NewErrInfo(e,options...)
}
}

@ -24,6 +24,7 @@
package errs
import (
"errs/fmts"
"fmt"
"time"
)
@ -81,17 +82,26 @@ func (e ErrInfo) Unwrap () error {
func (e ErrInfo) WrapWithCause(cause error) error {
return WrapWithCause(e,cause) }
func (e ErrInfo) WrapWithTrace(offset int) error {
return WrapWithTrace(e,offset) }
func (e ErrInfo) String () string {
return string(G.Formatter.Format(e)) }
func (e ErrInfo) BuildFormat (builder FormatBuilder) {
func (e ErrInfo) BuildFormat (builder fmts.FormatBuilder) {
builder.AddCode(e.Error())
builder.AddType("core.errs.ErrInfo")
builder.AddFieldString("Un texot a snake case ",e.message)
builder.AddFieldString("message",e.message)
builder.AddFieldString("info",e.info)
builder.AddFieldTime ("date",e.date)
@ -100,19 +110,11 @@ func (e ErrInfo) BuildFormat (builder FormatBuilder) {
builder.AddFieldString(n,v)
}
}
builder.AddWrapped (e.error)
builder.AddCause(UnwrapCause(e))
builder.AddTrace(UnwrapTrace(e))
}
func (e ErrInfo) ToErrCause (cause error) ErrCause {
return *NewErrCause(e,cause) }
@ -137,7 +139,6 @@ func ErrInfoWithCause (cause error) ErrInfoFnc {
}
func ErrInfoWithMessage(message string, params ...any) ErrInfoFnc {
if len(params) > 0 {
message = fmt.Sprintf(message,params ...)

@ -23,6 +23,7 @@
package errs
import "errs/fmts"
type (
@ -40,19 +41,31 @@ func NewErrPanic (err any) *ErrPanic {
}
func (e ErrPanic) Unwrap () error {
return e.error }
func (e ErrPanic) WrapWithInfo (options ...ErrInfoFnc) error {
return WrapWithInfo(e,options...) }
func (e ErrPanic) WrapWithCause(cause error) error {
return WrapWithCause(e,cause) }
func (e ErrPanic) WrapWithTrace(offset int) error {
return WrapWithTrace(e,offset) }
func (e ErrPanic) String () string {
return string(G.Formatter.Format(e)) }
func (e ErrPanic) BuildFormat (builder FormatBuilder) {
func (e ErrPanic) BuildFormat (builder fmts.FormatBuilder) {
builder.AddCode (e.Error())
builder.AddType ("core.errs.ErrPanic")
builder.AddWrapped(e.error)
}

@ -0,0 +1,87 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name err_trace.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
import (
"errs/fmts"
stack2 "errs/stack"
)
type (
ErrTrace struct {
error
stack stack2.Trace
}
)
func NewErrTrace (err error, offset int) *ErrTrace {
if err == nil {
return nil
}
var st stack2.Trace
tc,is := err.(stack2.Tracer)
if !is {
st = stack2.MakeStackTrace(offset)
} else {
st = tc.StackTrace()
}
return &ErrTrace{
error: err,
stack: st,
}
}
func (e ErrTrace) Unwrap() error {
return e.error }
func (e ErrTrace) StackTrace () stack2.Trace {
return e.stack }
func (e ErrTrace) WrapWithInfo (options ...ErrInfoFnc) error {
return WrapWithInfo(e,options...) }
func (e ErrTrace) WrapWithCause(cause error) error {
return WrapWithCause(e,cause) }
func (e ErrTrace) String () string {
return string(G.Formatter.Format(e)) }
func (e ErrTrace) BuildFormat (builder fmts.FormatBuilder) {
builder.AddCode(e.Error())
builder.AddType("core.errs.ErrTrace")
}

@ -0,0 +1,64 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name active.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 fmts
import (
"reflect"
"sync"
)
var (
G = struct {
mux sync.RWMutex
ErrBuilderCode string
ErrInvalidLevelCode string
typeBuilders map[reflect.Type]Formattable
extractors FormatExtractors
}{
ErrBuilderCode : "format-builder-error",
ErrInvalidLevelCode : "invalid-level",
typeBuilders : make(map[reflect.Type]Formattable),
extractors : DefaultsExtractors(),
}
)
func AddExtractor (ext ...FormatExtractor) {
G.mux.Lock()
G.extractors = append(G.extractors,ext...)
G.mux.Unlock()
}
func Extract (builder FormatBuilder, err error) {
G.mux.RLock()
G.extractors.Extract(builder,err)
G.mux.RUnlock()
}

@ -0,0 +1,61 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name err_format.go
// Created by DEV
// Modified 01/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 (
ErrFormat struct {
code string
cause error
}
)
func NewErrFormat (code string, cause error) *ErrFormat {
return &ErrFormat{
code : code,
cause : cause,
}
}
func (e ErrFormat) Code () string {
return e.code }
func (e ErrFormat) Error() string {
return e.code }
func (e ErrFormat) Cause() error {
return e.cause }
func (e ErrFormat) BuildFormat (builder FormatBuilder) {
builder.AddCode(e.Error())
builder.AddType("core.errs.fmts.ErrFormat")
}

@ -0,0 +1,41 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name field_extractor.go
// Created by DEV
// Modified 31/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
type (
FormatExtractor interface {
Extract (builder FormatBuilder, err error) bool
}
FormatExtractorFnc func (builder FormatBuilder, err error) bool
)
func (f FormatExtractorFnc) Extract (builder FormatBuilder, err error) bool {
return f(builder,err) }

@ -2,9 +2,9 @@
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name field_extractor.go
// File name extractors.go
// Created by DEV
// Modified 31/03/2024
// Modified 02/04/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
@ -21,28 +21,16 @@
// limitations under the License.
// ------------------------------------------------------------------------
package errs
package fmts
import "time"
type (
FormatExtractor interface {
Extract (builder FormatBuilder, err error) bool
}
FormatExtractorFnc func (builder FormatBuilder, err error) bool
FormatExtractors []FormatExtractor
)
func (f FormatExtractorFnc) Extract (builder FormatBuilder, err error) bool {
return f(builder,err) }
func (l FormatExtractors) Extract (builder FormatBuilder, err error) bool {
if err == nil || len(l) == 0 {
@ -100,4 +88,6 @@ func DefaultsExtractors () FormatExtractors {
return true
}),
}
}
}

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package errs
package fmts
import (
"atypes/astrings"
@ -64,18 +64,24 @@ type (
FieldNameColor FmtTextColor
FieldValueColor FmtTextColor
StringTransformers astrings.Transformers
ValueTransformers astrings.Transformers
NameTransformers astrings.Transformers
StringOnError string
StringOnCause string
StringOnWrapped string
StringOnTrace string
StringTransformers astrings.Transformers
ValueTransformers astrings.Transformers
NameTransformers astrings.Transformers
}
FmtTextFnc func (fm *FmtText)
FmtTextFnc func (fm *FmtText)
)
func NewFmtText(options ...FmtTextFnc) *FmtText {
f := &FmtText{
Show : FormatShowCause | FormatShowWrapped | FormatShowDetails | FormatShowDetails,
Indent : " ",
FieldSep : " : ",
@ -110,7 +116,7 @@ func (f *FmtText) Format (err error) []byte {
st.format(err)
if st.error != nil {
e := st.error
//st.Reset()
st.Reset()
st.format(e)
}

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package errs
package fmts
import "fmt"
@ -57,4 +57,5 @@ func (c FmtTextColor) Sprintf(fm string, params ...any) string {
func (c FmtTextColor) IsValid() bool {
return c >= FormatColorBlack && c <= FormatColorWhite }
return c >= FormatColorBlack && c <= FormatColorWhite
}

@ -21,22 +21,28 @@
// limitations under the License.
// ------------------------------------------------------------------------
package errs
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
}
)
@ -77,26 +83,46 @@ func (s *fmtTextState) AddCause (err error) {
return
}
s.addLevel()
defer s.decLevel()
s.writeString(s.indent)
s.writeString(s.f.StringOnCause)
s.format(err)
}
func (s *fmtTextState) AddTrace (trace StackTrace) {
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) {
s.writeField("error", s.f.ValueTransformers.Apply(code)) }
if s.isWrapped {
return
}
s.writeField("error", s.f.ValueTransformers.Apply(code))
}
func (s *fmtTextState) AddType (ter string ) {
s.writeField("error-type",ter) }
if s.isWrapped {
return
}
s.writeField("error-type",ter)
}
@ -134,12 +160,7 @@ func (s *fmtTextState) AddFieldDate (name string, value time.Time) {
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")
}
s.writeString(fmt.Sprintf("%s%s%s%s\n",s.indent,name,s.f.FieldSep,value))
}
@ -150,20 +171,24 @@ func (s *fmtTextState) writeString(str string) {
func (s *fmtTextState) format (er error) {
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()))
var ie error
if re := recover(); re != nil {
if ne,is := re.(error); is {
ie = ne
} else {
ie = fmt.Errorf(G.ErrBuilderCode,"%v",re)
}
}
s.error = *NewErrFormat(G.ErrBuilderCode,ie)
} else {
s.error = NewErrCause(G.ErrFormat,s.error)
s.error = *NewErrFormat(G.ErrBuilderCode,s.error)
}
} else if s.level != 0 {
s.error = G.ErrFormatLevel
}
if s.error != nil {
panic(s.error)
}
}(&dn, s)
@ -172,6 +197,19 @@ func (s *fmtTextState) format (er error) {
} else {
s.build(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
}
@ -192,13 +230,29 @@ func (s *fmtTextState) build (er error) {
s.AddType(rt.String())
Extract(s,er)
s.AddWrapped(Unwrap(er))
s.AddCause (UnwrapCause(er))
s.AddTrace (UnwrapTrace(er))
}
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)
}

@ -21,9 +21,12 @@
// limitations under the License.
// ------------------------------------------------------------------------
package errs
package fmts
import "time"
import (
"errs/stack"
"time"
)
type (
@ -41,8 +44,7 @@ type (
AddCode (code string)
AddType (ter string )
AddTrace (trace StackTrace)
AddTrace (trace stack.Trace)
AddString (str string)
AddStrings (str []string)

@ -0,0 +1,83 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name builders.go
// Created by DEV
// Modified 02/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
import (
"reflect"
"sync"
)
type (
FormatBuilders struct {
mux sync.RWMutex
builders map[reflect.Type]Formattable
}
FormatBuildersFnc func(builders *FormatBuilders)
)
func NewBuilders (options ...FormatBuildersFnc) *FormatBuilders {
b := &FormatBuilders{
builders: make(map[reflect.Type]Formattable),
}
for _,o:=range options {
o(b)
}
return b
}
func (b *FormatBuilders) Formattable (err error) Formattable {
if err == nil {
return nil
}
rt := reflect.TypeOf(err)
if rt.Kind() == reflect.Pointer {
rt = rt.Elem()
}
b.mux.Lock()
fm,ok := b.builders[rt]
if ok {
return fm
}
b.mux.Unlock()
return nil
}

@ -21,8 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package errs
package fmts
type (

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package errs
package fmts

@ -0,0 +1,58 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name active.go
// Created by DEV
// Modified 01/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 stack
import "errors"
const (
stackSkipFrames = 2
stackMaxFrames = 32
stackUnknownInfo = "???"
)
func UnwrapTrace(err error, cause bool) Trace {
if err == nil {
return nil
}
e := err
for e != nil {
if st,is := e.(Tracer); is {
return st.StackTrace()
}
e = errors.Unwrap(e)
}
if cause {
e = err
for e != nil {
if cs, is := e.(interface{ Cause() error }); is {
UnwrapTrace(cs.Cause(), cause)
}
e = errors.Unwrap(e)
}
}
return nil
}

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package errs
package stack
import (
"bufio"
@ -33,7 +33,7 @@ import (
type (
StackFrame struct {
Frame struct {
LineNumber int // Number line
File string // File FuncName
FuncName string // Function name
@ -44,11 +44,11 @@ type (
)
func NewStackFrame (counter uintptr) *StackFrame {
func NewFrame(counter uintptr) *Frame {
if counter == 0 {
return nil
}
sf := &StackFrame{
sf := &Frame{
Counter: counter,
}
fn := runtime.FuncForPC(counter)
@ -59,7 +59,7 @@ func NewStackFrame (counter uintptr) *StackFrame {
func (f StackFrame) SourceLine() (string, error) {
func (f Frame) SourceLine() (string, error) {
if f.LineNumber <= 0 {
return stackUnknownInfo, nil
}

@ -21,21 +21,23 @@
// limitations under the License.
// ------------------------------------------------------------------------
package errs
package stack
import "runtime"
import (
"runtime"
)
type (
StackTrace []uintptr
Trace []uintptr
)
func MakeStackTrace(offset int) StackTrace {
ls := make([]uintptr,stackMaxFrames)
func MakeStackTrace(offset int) Trace {
ls := make([]uintptr, stackMaxFrames)
if offset < 0 {
offset = 0
}
@ -46,10 +48,10 @@ func MakeStackTrace(offset int) StackTrace {
func (s StackTrace) Frames() []StackFrame {
f := make([]StackFrame,len(s))
func (s Trace) Frames() []Frame {
f := make([]Frame,len(s))
for i,p := range s {
f[i]=*NewStackFrame(p)
f[i]=*NewFrame(p)
}
return f
}

@ -21,16 +21,13 @@
// limitations under the License.
// ------------------------------------------------------------------------
package errs
package stack
type (
StackTracer interface {
Tracer interface {
StackTrace () StackTrace
StackTrace () Trace
}
)

@ -23,6 +23,9 @@
package errs
import (
"errs/stack"
)
type (
@ -51,4 +54,55 @@ func (f UnwrapperFnc) Unwrap () error {
func (f UnwrapperAllFnc) UnwrapAll () []error {
return f() }
return f() }
func Unwrap(err error) error {
if err == nil {
return nil
}
if cs,is := err.(Unwrapper); is {
return cs.Unwrap()
}
return nil
}
func UnwrapAll(err error) ErrorList {
if err == nil {
return nil
}
if al,is := err.(AllUnwrapper); is {
return al.UnwrapAll()
}
var l ErrorList
e := err
for e!= nil {
l = append(l,e)
e = Unwrap(e)
}
return l
}
func UnwrapCause(err error) error {
e := Unwrap(err)
for e != nil {
cs,is := e.(Causer)
if is {
return cs.Cause()
}
e = Unwrap(e)
}
return nil
}
func UnwrapTrace(err error) stack.Trace {
return stack.UnwrapTrace(err,false) }

@ -0,0 +1,72 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name wrappers.go
// Created by DEV
// Modified 01/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 errs
func Wrap (err, cause error) error {
if err == nil {
return cause
}
if cause == nil {
return err
}
return *NewErrCause(err, cause)
}
func WrapWithCause(err error, cause error) error {
if err == nil && cause == nil {
return nil
}
if err == nil {
return cause
}
if cause == nil {
return err
}
return *NewErrCause(err,cause)
}
func WrapWithInfo (err error, options ...ErrInfoFnc) error {
if err == nil {
return nil
}
return *NewErrInfo(err,options...)
}
func WrapWithTrace(err error, offset int) error {
if err == nil {
return nil
}
return *NewErrTrace(err,offset)
}
Loading…
Cancel
Save

Powered by TurnKey Linux.