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.
158 lines
2.9 KiB
158 lines
2.9 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
|
|
|
|
errCodeRT reflect.Type
|
|
errCauseRT reflect.Type
|
|
errTraceRT reflect.Type
|
|
errInfoRT reflect.Type
|
|
errPanicRT 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 {}),
|
|
}
|
|
)
|
|
|
|
|
|
|
|
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) ErrList {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
var l ErrList
|
|
e := err
|
|
for e!= nil {
|
|
l = append(l,e)
|
|
e = Unwrap(e)
|
|
}
|
|
return l
|
|
}
|
|
|
|
|
|
func UnwrapCause(err error) Causer {
|
|
e := Unwrap(err)
|
|
for e != nil {
|
|
cs,is := e.(Causer)
|
|
if is {
|
|
return cs
|
|
}
|
|
e = Unwrap(e)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
|
|
func UnwrapStackTrace(err error) StackTracer {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
e := err
|
|
for e != nil {
|
|
if st,is := e.(StackTracer); is {
|
|
return st
|
|
}
|
|
e = Unwrap(e)
|
|
}
|
|
return nil
|
|
}
|
|
|