]>
Commit | Line | Data |
---|---|---|
15c0b25d AP |
1 | // +build go1.7 |
2 | ||
3 | /* | |
4 | * | |
5 | * Copyright 2016 gRPC authors. | |
6 | * | |
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 | |
10 | * | |
11 | * http://www.apache.org/licenses/LICENSE-2.0 | |
12 | * | |
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. | |
18 | * | |
19 | */ | |
20 | ||
21 | package grpc | |
22 | ||
23 | import ( | |
24 | "context" | |
25 | "io" | |
26 | "net" | |
27 | "net/http" | |
28 | "os" | |
29 | ||
30 | netctx "golang.org/x/net/context" | |
31 | "google.golang.org/grpc/codes" | |
32 | "google.golang.org/grpc/status" | |
33 | "google.golang.org/grpc/transport" | |
34 | ) | |
35 | ||
36 | // dialContext connects to the address on the named network. | |
37 | func dialContext(ctx context.Context, network, address string) (net.Conn, error) { | |
38 | return (&net.Dialer{}).DialContext(ctx, network, address) | |
39 | } | |
40 | ||
41 | func sendHTTPRequest(ctx context.Context, req *http.Request, conn net.Conn) error { | |
42 | req = req.WithContext(ctx) | |
43 | if err := req.Write(conn); err != nil { | |
44 | return err | |
45 | } | |
46 | return nil | |
47 | } | |
48 | ||
49 | // toRPCErr converts an error into an error from the status package. | |
50 | func toRPCErr(err error) error { | |
51 | if _, ok := status.FromError(err); ok { | |
52 | return err | |
53 | } | |
54 | switch e := err.(type) { | |
55 | case transport.StreamError: | |
56 | return status.Error(e.Code, e.Desc) | |
57 | case transport.ConnectionError: | |
58 | return status.Error(codes.Unavailable, e.Desc) | |
59 | default: | |
60 | switch err { | |
61 | case context.DeadlineExceeded, netctx.DeadlineExceeded: | |
62 | return status.Error(codes.DeadlineExceeded, err.Error()) | |
63 | case context.Canceled, netctx.Canceled: | |
64 | return status.Error(codes.Canceled, err.Error()) | |
65 | case ErrClientConnClosing: | |
66 | return status.Error(codes.FailedPrecondition, err.Error()) | |
67 | } | |
68 | } | |
69 | return status.Error(codes.Unknown, err.Error()) | |
70 | } | |
71 | ||
72 | // convertCode converts a standard Go error into its canonical code. Note that | |
73 | // this is only used to translate the error returned by the server applications. | |
74 | func convertCode(err error) codes.Code { | |
75 | switch err { | |
76 | case nil: | |
77 | return codes.OK | |
78 | case io.EOF: | |
79 | return codes.OutOfRange | |
80 | case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF: | |
81 | return codes.FailedPrecondition | |
82 | case os.ErrInvalid: | |
83 | return codes.InvalidArgument | |
84 | case context.Canceled, netctx.Canceled: | |
85 | return codes.Canceled | |
86 | case context.DeadlineExceeded, netctx.DeadlineExceeded: | |
87 | return codes.DeadlineExceeded | |
88 | } | |
89 | switch { | |
90 | case os.IsExist(err): | |
91 | return codes.AlreadyExists | |
92 | case os.IsNotExist(err): | |
93 | return codes.NotFound | |
94 | case os.IsPermission(err): | |
95 | return codes.PermissionDenied | |
96 | } | |
97 | return codes.Unknown | |
98 | } |