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.
109 lines
1.9 KiB
109 lines
1.9 KiB
// ------------------------------------------------------------------------
|
|
// 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
|
|
|
|
import (
|
|
"errs/stack"
|
|
)
|
|
|
|
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() }
|
|
|
|
|
|
|
|
|
|
|
|
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) stack.Trace {
|
|
return stack.UnwrapTrace(err,false) }
|
|
|