You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
3.4 KiB

// ------------------------------------------------------------------------
// 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
ErrFormat ErrCode
ErrFormatLevel 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 : NewFormatText(),
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)),
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
}

Powered by TurnKey Linux.