parent
d96d15cb9b
commit
c23d60ba22
@ -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) }
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
@ -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…
Reference in new issue