github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/common/baderror/baderror.go (about)

     1  package baderror
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net"
     7  	"strings"
     8  
     9  	E "github.com/sagernet/sing/common/exceptions"
    10  )
    11  
    12  func Contains(err error, msgList ...string) bool {
    13  	for _, msg := range msgList {
    14  		if strings.Contains(err.Error(), msg) {
    15  			return true
    16  		}
    17  	}
    18  	return false
    19  }
    20  
    21  func WrapH2(err error) error {
    22  	if err == nil {
    23  		return nil
    24  	}
    25  	err = E.Unwrap(err)
    26  	if err == io.ErrUnexpectedEOF {
    27  		return io.EOF
    28  	}
    29  	if Contains(err, "client disconnected", "body closed by handler", "response body closed", "; CANCEL") {
    30  		return net.ErrClosed
    31  	}
    32  	return err
    33  }
    34  
    35  func WrapGRPC(err error) error {
    36  	// grpc uses stupid internal error types
    37  	if err == nil {
    38  		return nil
    39  	}
    40  	if Contains(err, "EOF") {
    41  		return io.EOF
    42  	}
    43  	if Contains(err, "Canceled") {
    44  		return context.Canceled
    45  	}
    46  	if Contains(err,
    47  		"the client connection is closing",
    48  		"server closed the stream without sending trailers") {
    49  		return net.ErrClosed
    50  	}
    51  	return err
    52  }
    53  
    54  func WrapQUIC(err error) error {
    55  	if err == nil {
    56  		return nil
    57  	}
    58  	if Contains(err, "canceled by local with error code 0") {
    59  		return net.ErrClosed
    60  	}
    61  	return err
    62  }