go16.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // +build go1.6,!go1.7
  2. /*
  3. *
  4. * Copyright 2018 gRPC authors.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package status
  20. import (
  21. "golang.org/x/net/context"
  22. "google.golang.org/grpc/codes"
  23. )
  24. // FromContextError converts a context error into a Status. It returns a
  25. // Status with codes.OK if err is nil, or a Status with codes.Unknown if err is
  26. // non-nil and not a context error.
  27. func FromContextError(err error) *Status {
  28. switch err {
  29. case nil:
  30. return New(codes.OK, "")
  31. case context.DeadlineExceeded:
  32. return New(codes.DeadlineExceeded, err.Error())
  33. case context.Canceled:
  34. return New(codes.Canceled, err.Error())
  35. default:
  36. return New(codes.Unknown, err.Error())
  37. }
  38. }