go17.go 1.2 KB

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