2022-08-29 10:10:41 +08:00
|
|
|
package baderror
|
|
|
|
|
2022-09-07 13:23:03 +08:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
)
|
2022-08-29 10:10:41 +08:00
|
|
|
|
|
|
|
func Contains(err error, msgList ...string) bool {
|
|
|
|
for _, msg := range msgList {
|
|
|
|
if strings.Contains(err.Error(), msg) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-09-07 13:23:03 +08:00
|
|
|
|
|
|
|
func WrapH2(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = E.Unwrap(err)
|
|
|
|
if err == io.ErrUnexpectedEOF {
|
|
|
|
return io.EOF
|
|
|
|
}
|
2022-09-15 15:34:52 +08:00
|
|
|
if Contains(err, "client disconnected", "body closed by handler", "response body closed", "; CANCEL") {
|
2022-09-07 13:23:03 +08:00
|
|
|
return net.ErrClosed
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func WrapGRPC(err error) error {
|
|
|
|
// grpc uses stupid internal error types
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if Contains(err, "EOF") {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
if Contains(err, "Canceled") {
|
|
|
|
return context.Canceled
|
|
|
|
}
|
|
|
|
if Contains(err,
|
|
|
|
"the client connection is closing",
|
|
|
|
"server closed the stream without sending trailers") {
|
|
|
|
return net.ErrClosed
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func WrapQUIC(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if Contains(err, "canceled with error code 0") {
|
|
|
|
return net.ErrClosed
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|