5 * Copyright 2016 gRPC authors.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
31 netctx "golang.org/x/net/context"
32 "google.golang.org/grpc/codes"
33 "google.golang.org/grpc/status"
34 "google.golang.org/grpc/transport"
37 // dialContext connects to the address on the named network.
38 func dialContext(ctx context.Context, network, address string) (net.Conn, error) {
39 return (&net.Dialer{}).DialContext(ctx, network, address)
42 func sendHTTPRequest(ctx context.Context, req *http.Request, conn net.Conn) error {
43 req = req.WithContext(ctx)
44 if err := req.Write(conn); err != nil {
45 return fmt.Errorf("failed to write the HTTP request: %v", err)
50 // toRPCErr converts an error into an error from the status package.
51 func toRPCErr(err error) error {
52 if _, ok := status.FromError(err); ok {
55 switch e := err.(type) {
56 case transport.StreamError:
57 return status.Error(e.Code, e.Desc)
58 case transport.ConnectionError:
59 return status.Error(codes.Unavailable, e.Desc)
62 case context.DeadlineExceeded, netctx.DeadlineExceeded:
63 return status.Error(codes.DeadlineExceeded, err.Error())
64 case context.Canceled, netctx.Canceled:
65 return status.Error(codes.Canceled, err.Error())
66 case ErrClientConnClosing:
67 return status.Error(codes.FailedPrecondition, err.Error())
70 return status.Error(codes.Unknown, err.Error())
73 // convertCode converts a standard Go error into its canonical code. Note that
74 // this is only used to translate the error returned by the server applications.
75 func convertCode(err error) codes.Code {
80 return codes.OutOfRange
81 case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF:
82 return codes.FailedPrecondition
84 return codes.InvalidArgument
85 case context.Canceled, netctx.Canceled:
87 case context.DeadlineExceeded, netctx.DeadlineExceeded:
88 return codes.DeadlineExceeded
92 return codes.AlreadyExists
93 case os.IsNotExist(err):
95 case os.IsPermission(err):
96 return codes.PermissionDenied