github.com/annwntech/go-micro/v2@v2.9.5/server/grpc/util.go (about)

     1  package grpc
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"os"
     7  	"sync"
     8  
     9  	"google.golang.org/grpc/codes"
    10  )
    11  
    12  // convertCode converts a standard Go error into its canonical code. Note that
    13  // this is only used to translate the error returned by the server applications.
    14  func convertCode(err error) codes.Code {
    15  	switch err {
    16  	case nil:
    17  		return codes.OK
    18  	case io.EOF:
    19  		return codes.OutOfRange
    20  	case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF:
    21  		return codes.FailedPrecondition
    22  	case os.ErrInvalid:
    23  		return codes.InvalidArgument
    24  	case context.Canceled:
    25  		return codes.Canceled
    26  	case context.DeadlineExceeded:
    27  		return codes.DeadlineExceeded
    28  	}
    29  	switch {
    30  	case os.IsExist(err):
    31  		return codes.AlreadyExists
    32  	case os.IsNotExist(err):
    33  		return codes.NotFound
    34  	case os.IsPermission(err):
    35  		return codes.PermissionDenied
    36  	}
    37  	return codes.Unknown
    38  }
    39  
    40  func wait(ctx context.Context) *sync.WaitGroup {
    41  	if ctx == nil {
    42  		return nil
    43  	}
    44  	wg, ok := ctx.Value("wait").(*sync.WaitGroup)
    45  	if !ok {
    46  		return nil
    47  	}
    48  	return wg
    49  }