parent
15852b0712
commit
2280bea931
@ -0,0 +1,171 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 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 errs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
|
||||
const (
|
||||
|
||||
stackSkipFrames = 2
|
||||
stackMaxFrames = 32
|
||||
|
||||
stackUnknownInfo = "???"
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
var (
|
||||
G = struct {
|
||||
|
||||
Formatter Formatter
|
||||
|
||||
ErrGroup ErrCode
|
||||
ErrPanic ErrCode
|
||||
ErrParamNil ErrCode
|
||||
ErrParamInvalid ErrCode
|
||||
ErrParamOutOfRange ErrCode
|
||||
|
||||
errCodeRT reflect.Type
|
||||
errCauseRT reflect.Type
|
||||
errTraceRT reflect.Type
|
||||
errInfoRT reflect.Type
|
||||
errPanicRT reflect.Type
|
||||
errGroupRT reflect.Type
|
||||
errMultiRT reflect.Type
|
||||
|
||||
stackTraceRType reflect.Type
|
||||
}{
|
||||
|
||||
//Formatter : NewFmtText(),
|
||||
|
||||
ErrGroup : ErrCode("group-error"),
|
||||
ErrPanic : ErrCode("panic-error"),
|
||||
ErrParamNil : ErrCode("param-nil"),
|
||||
ErrParamInvalid : ErrCode("param-invalid"),
|
||||
ErrParamOutOfRange : ErrCode("param-out-of-range"),
|
||||
|
||||
stackTraceRType : reflect.TypeOf(StackTrace(nil)),
|
||||
|
||||
errCodeRT : reflect.TypeOf(ErrCode("")),
|
||||
errCauseRT : reflect.TypeOf(ErrCause{}),
|
||||
/*
|
||||
errTraceRT : reflect.TypeOf(ErrTrace{}),
|
||||
errPanicRT : reflect.TypeOf(ErrPanic{}),
|
||||
errInfoRT : reflect.TypeOf(ErrInfo {}),
|
||||
errGroupRT : reflect.TypeOf(ErrGroup{}),
|
||||
errMultiRT : reflect.TypeOf(ErrMulti(nil)),
|
||||
|
||||
*/
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
func ToErr (err any) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
switch e := err.(type) {
|
||||
case error : return e
|
||||
//case []error: return MakeErrGroup(e...)
|
||||
default : return fmt.Errorf("%v",err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func Wrap (err, cause error) error {
|
||||
if err == nil {
|
||||
return cause
|
||||
}
|
||||
if cause == nil {
|
||||
return err
|
||||
}
|
||||
return *NewErrCause(err, cause)
|
||||
}
|
||||
|
||||
|
||||
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) 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
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,42 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name causer.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 (
|
||||
|
||||
Causer interface {
|
||||
|
||||
Cause () error
|
||||
}
|
||||
|
||||
|
||||
CauseFnc func () error
|
||||
)
|
||||
|
||||
|
||||
func (f CauseFnc) Cause() error {
|
||||
return f() }
|
||||
|
||||
@ -0,0 +1,125 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name errMeta.go
|
||||
// Created by DEV
|
||||
// Modified 24/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 (
|
||||
|
||||
ErrInfo struct {
|
||||
error
|
||||
message string
|
||||
info string
|
||||
date time.Time
|
||||
metas map[string]string
|
||||
}
|
||||
|
||||
ErrInfoFnc func (*ErrInfo)
|
||||
|
||||
)
|
||||
|
||||
|
||||
func NewErrInfo(err error, options ...ErrInfoFnc) *ErrInfo {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return applyErrOptions(&ErrInfo{
|
||||
error: err,
|
||||
date : time.Now(),
|
||||
}, options ...)
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (e ErrInfo) Info () string {
|
||||
return e.info }
|
||||
|
||||
func (e ErrInfo) Date () time.Time {
|
||||
return e.date }
|
||||
|
||||
func (e ErrInfo) Message () string {
|
||||
return e.message }
|
||||
|
||||
|
||||
func (e ErrInfo) MapMetas(f func (name string, value any) bool) {
|
||||
if len(e.metas) > 0 {
|
||||
for k,v := range e.metas {
|
||||
if !f(k,v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (e ErrInfo) Unwrap () error {
|
||||
return e.error }
|
||||
|
||||
|
||||
|
||||
|
||||
func (e ErrInfo) Format (builder FormatBuilder) {
|
||||
o := builder.Options()
|
||||
n := e.Unwrap()
|
||||
if !o.ShowWrapped() {
|
||||
e.formatWrapped(builder, n)
|
||||
return
|
||||
}
|
||||
|
||||
builder.AddCode(e.Error())
|
||||
builder.AddType("core.errs.ErrInfo")
|
||||
|
||||
e.formatWrapped(builder, n)
|
||||
|
||||
if o.ShowCause() {
|
||||
if c := UnwrapCause(n); c != nil {
|
||||
builder.AddCause(c)
|
||||
}
|
||||
}
|
||||
if o.ShowTrace() {
|
||||
if t := UnwrapTrace(n); t != nil {
|
||||
builder.AddTrace(t)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (e ErrInfo) formatWrapped (builder FormatBuilder, next error) {
|
||||
if builder.Options().ShowDetails() {
|
||||
builder.AddFieldString("message",e.message)
|
||||
builder.AddFieldString("info",e.info)
|
||||
builder.AddFieldTime ("date",e.date)
|
||||
if len(e.metas) > 0 {
|
||||
for n,v := range e.metas {
|
||||
builder.AddFieldString(n,v)
|
||||
}
|
||||
}
|
||||
if next != nil {
|
||||
builder.AddWrapped(next)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,87 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name err_ops.go
|
||||
// Created by DEV
|
||||
// Modified 24/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"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func applyErrOptions(Err *ErrInfo, options ...ErrInfoFnc) *ErrInfo {
|
||||
for _,o := range options {
|
||||
o(Err)
|
||||
}
|
||||
return Err
|
||||
}
|
||||
|
||||
|
||||
|
||||
func ErrInfoWithCause (cause error) ErrInfoFnc {
|
||||
return func(e *ErrInfo) {
|
||||
if cause == nil {
|
||||
return
|
||||
}
|
||||
e.error = *NewErrCause(e.error,cause)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func ErrInfoWithMessage(message string, params ...any) ErrInfoFnc {
|
||||
if len(params) > 0 {
|
||||
message = fmt.Sprintf(message,params ...)
|
||||
}
|
||||
return func(e *ErrInfo) {
|
||||
e.message = message
|
||||
}
|
||||
}
|
||||
|
||||
func ErrInfoWithInfo (info string, params ...any) ErrInfoFnc {
|
||||
if len(params) > 0 {
|
||||
info = fmt.Sprintf(info,params ...)
|
||||
}
|
||||
return func(e *ErrInfo) {
|
||||
e.info = info
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func ErrInfoWithStackTrace (offset int) ErrInfoFnc {
|
||||
return func (e *ErrInfo) {
|
||||
e.error = *NewErrTrace(e.error,offset) }
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
func ErrInfoWithMeta (name string, value string) ErrInfoFnc {
|
||||
return func (e *ErrInfo) {
|
||||
if e.metas == nil {
|
||||
e.metas=map[string]string{name:value}
|
||||
return
|
||||
}
|
||||
e.metas[name]=value
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,54 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name err_panic.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 (
|
||||
|
||||
ErrPanic struct {
|
||||
error
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func NewErrPanic (err any) *ErrPanic {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return &ErrPanic{ error: ToErr(err) }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (e ErrPanic) Unwrap () error {
|
||||
return e.error }
|
||||
|
||||
|
||||
|
||||
func (e ErrPanic) Format (builder FormatBuilder) {
|
||||
|
||||
builder.AddCode(e.Error())
|
||||
builder.AddType("core.errs.ErrPanic")
|
||||
}
|
||||
@ -0,0 +1,100 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name error_list.go
|
||||
// Created by DEV
|
||||
// Modified 21/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 (
|
||||
|
||||
ErrorList []error
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
func (l ErrorList) Any () bool {
|
||||
for _,e := range l {
|
||||
if e != nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
func (l ErrorList) Len () int {
|
||||
return len(l) }
|
||||
|
||||
|
||||
func (l ErrorList) Count () int {
|
||||
c:=0
|
||||
for _,e := range l {
|
||||
if e != nil {
|
||||
c++
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
|
||||
func (l ErrorList) First () error {
|
||||
for _,e := range l {
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l ErrorList) Last () error {
|
||||
for i := len(l) - 1; i >= 0; i-- {
|
||||
if l[i] != nil {
|
||||
return l[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Clean removes any nil errors from the list.
|
||||
func (l ErrorList) Clean() ErrorList {
|
||||
return l.filter(func(e error) bool {
|
||||
return e != nil }) }
|
||||
|
||||
|
||||
func (l ErrorList) filter(filter func(error) bool) ErrorList {
|
||||
rs := make([]error, 0, len(l))
|
||||
for _, e := range l {
|
||||
if filter(e) {
|
||||
rs = append(rs, e)
|
||||
}
|
||||
}
|
||||
if len(rs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return rs
|
||||
}
|
||||
@ -0,0 +1,83 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 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 errs
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
FormatText struct {
|
||||
options *FormatTextOps
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
func NewFormatText (options ...any) *FormatText {
|
||||
f := &FormatText{
|
||||
options: NewFormatTextOps(options...),
|
||||
}
|
||||
return f
|
||||
|
||||
}
|
||||
|
||||
|
||||
func (f *FormatText) Format (err error) (r []byte) {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
dn := false
|
||||
st := newFormatTextState (f.options)
|
||||
|
||||
defer func(d *bool, s *formatTextState){
|
||||
st.Reset()
|
||||
if !(*d) {
|
||||
if e := NewErrPanic(recover()); e != nil {
|
||||
e.Format()
|
||||
}
|
||||
}
|
||||
}(&dn,st)
|
||||
|
||||
|
||||
if bl,is := err.(Formattable); is {
|
||||
bl.Build(st)
|
||||
} else {
|
||||
f.build (st,err)
|
||||
}
|
||||
dn = true
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func (f *FormatText) build (s *formatTextState, err error) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,54 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name format_text_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
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
FormatTextOps struct {
|
||||
FormatOps
|
||||
}
|
||||
|
||||
FormatTextOpsFnc func (ops *FormatTextOps)
|
||||
)
|
||||
|
||||
|
||||
func NewFormatTextOps (options ...any) *FormatTextOps {
|
||||
os := &FormatTextOps {
|
||||
FormatOps: *NewFormatOps(),
|
||||
}
|
||||
for i,o := range options {
|
||||
switch t := o.(type) {
|
||||
case func(ops *FormatTextOps): t(os)
|
||||
case func(ops *FormatOps) : t(&os.FormatOps)
|
||||
default:
|
||||
//todo errPanic invalid parameter
|
||||
}
|
||||
}
|
||||
return os
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,89 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 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 errs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
formatTextState struct {
|
||||
options *FormatTextOps
|
||||
buffer bytes.Buffer
|
||||
level int
|
||||
error error
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
func newFormatTextState (options *FormatTextOps) *formatTextState {
|
||||
st := &formatTextState{
|
||||
options : options,
|
||||
}
|
||||
return st
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func (f *formatTextState) Options() FormatOps {
|
||||
return f.options.FormatOps }
|
||||
|
||||
func (f *formatTextState) Reset () {}
|
||||
|
||||
func (f *formatTextState) Level () int {
|
||||
return f.level }
|
||||
|
||||
func (f *formatTextState) Error () error {
|
||||
return f.error }
|
||||
|
||||
|
||||
func (f *formatTextState) Bytes () []byte {
|
||||
return f.buffer.Bytes() }
|
||||
|
||||
func (f *formatTextState) AddError (err error, options ...FormatOpsFnc) {}
|
||||
func (f *formatTextState) AddCause (err error, options ...FormatOpsFnc) {}
|
||||
func (f *formatTextState) AddWrapped(err error, options ...FormatOpsFnc) {}
|
||||
|
||||
func (f *formatTextState) AddCode (code string, options ...FormatOpsFnc) {}
|
||||
func (f *formatTextState) AddType (ter string , options ...FormatOpsFnc) {}
|
||||
|
||||
|
||||
func (f *formatTextState) AddTrace (trace StackTrace, options ...FormatOpsFnc) {}
|
||||
|
||||
func (f *formatTextState) AddString (str string, options ...FormatOpsFnc) {}
|
||||
func (f *formatTextState) AddStringLn (str string, options ...FormatOpsFnc) {}
|
||||
|
||||
func (f *formatTextState) AddFieldString (name string, value string , options ...FormatOpsFnc) {}
|
||||
func (f *formatTextState) AddFieldInt (name string, value int , options ...FormatOpsFnc) {}
|
||||
func (f *formatTextState) AddFieldFloat (name string, value float64 , options ...FormatOpsFnc) {}
|
||||
func (f *formatTextState) AddFieldDate (name string, value time.Time, options ...FormatOpsFnc) {}
|
||||
func (f *formatTextState) AddFieldTime (name string, value time.Time, options ...FormatOpsFnc) {}
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,102 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stack_frame.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
StackFrame struct {
|
||||
LineNumber int // Number line
|
||||
File string // File FuncName
|
||||
FuncName string // Function name
|
||||
Package string // Package name
|
||||
Counter uintptr
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
|
||||
func NewStackFrame (counter uintptr) *StackFrame {
|
||||
if counter == 0 {
|
||||
return nil
|
||||
}
|
||||
sf := &StackFrame{
|
||||
Counter: counter,
|
||||
}
|
||||
fn := runtime.FuncForPC(counter)
|
||||
sf.Package, sf.FuncName = packageAndName(fn)
|
||||
sf.File, sf.LineNumber = fn.FileLine(counter-1)
|
||||
return sf
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (f StackFrame) SourceLine() (string, error) {
|
||||
if f.LineNumber <= 0 {
|
||||
return stackUnknownInfo, nil
|
||||
}
|
||||
|
||||
file, err := os.Open(f.File)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
currentLine := 1
|
||||
for scanner.Scan() {
|
||||
if currentLine == f.LineNumber {
|
||||
return string(bytes.Trim(scanner.Bytes(), " \t")), nil
|
||||
}
|
||||
currentLine++
|
||||
}
|
||||
_ = file.Close()
|
||||
if err := scanner.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return stackUnknownInfo, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
func packageAndName (fn *runtime.Func) (string,string) {
|
||||
nm := fn.Name()
|
||||
pg := ""
|
||||
if ls := strings.LastIndex(nm,"/"); ls >= 0 {
|
||||
pg = nm[:ls] + "/"
|
||||
nm = nm[ls+1:]
|
||||
}
|
||||
if pd := strings.Index(nm,"."); pd >= 0 {
|
||||
pg += nm[:pd]
|
||||
nm = nm[pd+1:]
|
||||
}
|
||||
return pg,strings.Replace(nm,"·", ".",-1)
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stack_trace.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
|
||||
|
||||
import "runtime"
|
||||
|
||||
type (
|
||||
|
||||
|
||||
StackTrace []uintptr
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
func MakeStackTrace(offset int) StackTrace {
|
||||
ls := make([]uintptr,stackMaxFrames)
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
cn := runtime.Callers(offset+stackSkipFrames,ls)
|
||||
return ls[:cn]
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (s StackTrace) Frames() []StackFrame {
|
||||
f := make([]StackFrame,len(s))
|
||||
for i,p := range s {
|
||||
f[i]=*NewStackFrame(p)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,40 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stack_tracer.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 (
|
||||
|
||||
StackTracer interface {
|
||||
|
||||
StackTrace () StackTrace
|
||||
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,54 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name unwraper.go
|
||||
// Created by DEV
|
||||
// Modified 21/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 (
|
||||
|
||||
Unwrapper interface {
|
||||
Unwrap () error
|
||||
}
|
||||
|
||||
UnwrapperFnc func () error
|
||||
|
||||
|
||||
|
||||
AllUnwrapper interface {
|
||||
UnwrapAll () []error
|
||||
}
|
||||
|
||||
UnwrapperAllFnc func () []error
|
||||
|
||||
|
||||
)
|
||||
|
||||
|
||||
func (f UnwrapperFnc) Unwrap () error {
|
||||
return f() }
|
||||
|
||||
|
||||
|
||||
|
||||
func (f UnwrapperAllFnc) UnwrapAll () []error {
|
||||
return f() }
|
||||
Loading…
Reference in new issue