Skip to content

Commit

Permalink
Fix errors usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 16, 2024
1 parent 33beacc commit aa7d254
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 28 deletions.
6 changes: 2 additions & 4 deletions common/baderror/baderror.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package baderror

import (
"context"
"errors"
"io"
"net"
"strings"

E "github.com/sagernet/sing/common/exceptions"
)

func Contains(err error, msgList ...string) bool {
Expand All @@ -22,8 +21,7 @@ func WrapH2(err error) error {
if err == nil {
return nil
}
err = E.Unwrap(err)
if err == io.ErrUnexpectedEOF {
if errors.Is(err, io.ErrUnexpectedEOF) {
return io.EOF
}
if Contains(err, "client disconnected", "body closed by handler", "response body closed", "; CANCEL") {
Expand Down
22 changes: 6 additions & 16 deletions common/exceptions/inner.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
package exceptions

import "github.com/sagernet/sing/common"
import (
"errors"

type HasInnerError interface {
Unwrap() error
}
"github.com/sagernet/sing/common"
)

// Deprecated: Use errors.Unwrap instead.
func Unwrap(err error) error {
for {
inner, ok := err.(HasInnerError)
if !ok {
break
}
innerErr := inner.Unwrap()
if innerErr == nil {
break
}
err = innerErr
}
return err
return errors.Unwrap(err)
}

func Cast[T any](err error) (T, bool) {
Expand Down
9 changes: 1 addition & 8 deletions common/exceptions/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,5 @@ func IsMulti(err error, targetList ...error) bool {
return true
}
}
err = Unwrap(err)
multiErr, isMulti := err.(MultiError)
if !isMulti {
return false
}
return common.All(multiErr.Unwrap(), func(it error) bool {
return IsMulti(it, targetList...)
})
return false
}

0 comments on commit aa7d254

Please sign in to comment.