1// +build 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
21package status
22
23import (
24	"context"
25
26	netctx "golang.org/x/net/context"
27	"google.golang.org/grpc/codes"
28)
29
30// FromContextError converts a context error into a Status.  It returns a
31// Status with codes.OK if err is nil, or a Status with codes.Unknown if err is
32// non-nil and not a context error.
33func FromContextError(err error) *Status {
34	switch err {
35	case nil:
36		return New(codes.OK, "")
37	case context.DeadlineExceeded, netctx.DeadlineExceeded:
38		return New(codes.DeadlineExceeded, err.Error())
39	case context.Canceled, netctx.Canceled:
40		return New(codes.Canceled, err.Error())
41	default:
42		return New(codes.Unknown, err.Error())
43	}
44}
45