mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-03 11:57:39 +03:00
Fix errors usage
This commit is contained in:
parent
33beacc053
commit
aa7d2543a3
3 changed files with 9 additions and 28 deletions
|
@ -2,11 +2,10 @@ package baderror
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Contains(err error, msgList ...string) bool {
|
func Contains(err error, msgList ...string) bool {
|
||||||
|
@ -22,8 +21,7 @@ func WrapH2(err error) error {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = E.Unwrap(err)
|
if errors.Is(err, io.ErrUnexpectedEOF) {
|
||||||
if err == io.ErrUnexpectedEOF {
|
|
||||||
return io.EOF
|
return io.EOF
|
||||||
}
|
}
|
||||||
if Contains(err, "client disconnected", "body closed by handler", "response body closed", "; CANCEL") {
|
if Contains(err, "client disconnected", "body closed by handler", "response body closed", "; CANCEL") {
|
||||||
|
|
|
@ -1,24 +1,14 @@
|
||||||
package exceptions
|
package exceptions
|
||||||
|
|
||||||
import "github.com/sagernet/sing/common"
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
type HasInnerError interface {
|
"github.com/sagernet/sing/common"
|
||||||
Unwrap() error
|
)
|
||||||
}
|
|
||||||
|
|
||||||
|
// Deprecated: Use errors.Unwrap instead.
|
||||||
func Unwrap(err error) error {
|
func Unwrap(err error) error {
|
||||||
for {
|
return errors.Unwrap(err)
|
||||||
inner, ok := err.(HasInnerError)
|
|
||||||
if !ok {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
innerErr := inner.Unwrap()
|
|
||||||
if innerErr == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
err = innerErr
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Cast[T any](err error) (T, bool) {
|
func Cast[T any](err error) (T, bool) {
|
||||||
|
|
|
@ -63,12 +63,5 @@ func IsMulti(err error, targetList ...error) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = Unwrap(err)
|
return false
|
||||||
multiErr, isMulti := err.(MultiError)
|
|
||||||
if !isMulti {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return common.All(multiErr.Unwrap(), func(it error) bool {
|
|
||||||
return IsMulti(it, targetList...)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue