goext/exerr/helper.go

89 lines
1.5 KiB
Go
Raw Permalink Normal View History

2023-07-24 10:42:39 +02:00
package exerr
import "fmt"
// IsType test if the supplied error is of the specified ErrorType.
func IsType(err error, errType ErrorType) bool {
if err == nil {
return false
}
2023-07-24 11:11:15 +02:00
bmerr := FromError(err)
2023-07-24 10:42:39 +02:00
for bmerr != nil {
if bmerr.Type == errType {
return true
}
bmerr = bmerr.OriginalError
}
return false
}
// IsFrom test if the supplied error stems originally from original
func IsFrom(e error, original error) bool {
if e == nil {
return false
}
//goland:noinspection GoDirectComparisonOfErrors
2023-07-24 10:42:39 +02:00
if e == original {
return true
}
2023-07-24 11:11:15 +02:00
bmerr := FromError(e)
2023-07-24 10:42:39 +02:00
for bmerr == nil {
return false
}
for curr := bmerr; curr != nil; curr = curr.OriginalError {
if curr.Category == CatForeign && curr.Message == original.Error() && curr.WrappedErrType == fmt.Sprintf("%T", original) {
return true
}
}
return false
}
// HasSourceMessage tests if the supplied error stems originally from an error with the message msg
func HasSourceMessage(e error, msg string) bool {
if e == nil {
return false
}
2023-07-24 11:11:15 +02:00
bmerr := FromError(e)
2023-07-24 10:42:39 +02:00
for bmerr == nil {
return false
}
for curr := bmerr; curr != nil; curr = curr.OriginalError {
if curr.OriginalError == nil && curr.Message == msg {
return true
}
}
return false
}
func MessageMatch(e error, matcher func(string) bool) bool {
if e == nil {
return false
}
if matcher(e.Error()) {
return true
}
2023-07-24 11:11:15 +02:00
bmerr := FromError(e)
2023-07-24 10:42:39 +02:00
for bmerr == nil {
return false
}
for curr := bmerr; curr != nil; curr = curr.OriginalError {
if matcher(curr.Message) {
return true
}
}
return false
}