Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | // +build go1.6,!go1.7 |
| 2 | |
| 3 | /* |
| 4 | * |
| 5 | * Copyright 2018 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 status |
| 22 | |
| 23 | import ( |
| 24 | "golang.org/x/net/context" |
| 25 | "google.golang.org/grpc/codes" |
| 26 | ) |
| 27 | |
| 28 | // FromContextError converts a context error into a Status. It returns a |
| 29 | // Status with codes.OK if err is nil, or a Status with codes.Unknown if err is |
| 30 | // non-nil and not a context error. |
| 31 | func FromContextError(err error) *Status { |
| 32 | switch err { |
| 33 | case nil: |
| 34 | return New(codes.OK, "") |
| 35 | case context.DeadlineExceeded: |
| 36 | return New(codes.DeadlineExceeded, err.Error()) |
| 37 | case context.Canceled: |
| 38 | return New(codes.Canceled, err.Error()) |
| 39 | default: |
| 40 | return New(codes.Unknown, err.Error()) |
| 41 | } |
| 42 | } |