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.

149 lines
2.7 KiB

// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name gogo.go
// Created by DEV
// Modified 16/01/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 (
"errors"
"fmt"
)
const (
stackMaxFrames = 32
stackSkipFrames = 2
)
const (
traceUnknown string = "??"
)
var (
G = struct {
ErrPanic ErrCode
ErrParamNil ErrCode
ErrParamInvalid ErrCode
ErrParamOutOfRange ErrCode
}{
ErrPanic: ErrCode("core:errs.panic-error"),
ErrParamNil: ErrCode("core:errs.param-nil"),
ErrParamInvalid: ErrCode("core:errs.param-invalid"),
ErrParamOutOfRange: ErrCode("core:errs.param-out-of-range"),
}
)
func Is(err, target error) bool {
return errors.Is(err, target) }
func As(err error, target any) bool {
return errors.As(err, target) }
func ToError(err any) error {
if err == nil {
return nil
}
switch e := err.(type) {
case error:
return e
case []error:
return Errs(e).ToError()
default:
return ErrCode(fmt.Sprintf("#{er}"))
}
}
func ToPanic(err any) error {
if err == nil {
return nil
}
return G.ErrPanic.WithCause(ToError(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 {
cs, is := err.(_W)
if !is {
return nil
}
return cs.Unwrap()
}
func getStackTrace(err error) StackTrace {
for err != nil {
switch e := err.(type) {
case StackTracer:
return StackTrace(e.Stack())
case _T:
return StackTrace(e.StackTrace())
case _C:
return StackTrace(e.Callers())
case _F:
return StackTrace(e.StackFrames())
}
err = Unwrap(err)
}
return nil
}
func String (err error) string {
if err == nil {
return ""
}
switch err.(type) {
default:
return err.Error()
}
}
func Test(err error, halt bool) {
if err != nil {
//panic(err)
ei := NewErrInfo(err,ErrInfoWithStack())
if halt {
panic(ei.Error())
}
}
}

Powered by TurnKey Linux.