|
| 1 | +// Copyright 2018, OpenCensus Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "opencensus/common/internal/grpc/status.h" |
| 16 | + |
| 17 | +#include <string> |
| 18 | + |
| 19 | +#include "absl/strings/str_cat.h" |
| 20 | +#include "absl/strings/string_view.h" |
| 21 | +#include "include/grpc++/support/status.h" |
| 22 | + |
| 23 | +namespace opencensus { |
| 24 | +namespace common { |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +absl::string_view StatusCodeName(grpc::StatusCode code) { |
| 29 | + switch (code) { |
| 30 | + case grpc::StatusCode::OK: |
| 31 | + return "OK"; |
| 32 | + case grpc::StatusCode::CANCELLED: |
| 33 | + return "CANCELLED"; |
| 34 | + case grpc::StatusCode::UNKNOWN: |
| 35 | + return "UNKNOWN"; |
| 36 | + case grpc::StatusCode::INVALID_ARGUMENT: |
| 37 | + return "INVALID_ARGUMENT"; |
| 38 | + case grpc::StatusCode::DEADLINE_EXCEEDED: |
| 39 | + return "DEADLINE_EXCEEDED"; |
| 40 | + case grpc::StatusCode::NOT_FOUND: |
| 41 | + return "NOT_FOUND"; |
| 42 | + case grpc::StatusCode::ALREADY_EXISTS: |
| 43 | + return "ALREADY_EXISTS"; |
| 44 | + case grpc::StatusCode::PERMISSION_DENIED: |
| 45 | + return "PERMISSION_DENIED"; |
| 46 | + case grpc::StatusCode::RESOURCE_EXHAUSTED: |
| 47 | + return "RESOURCE_EXHAUSTED"; |
| 48 | + case grpc::StatusCode::FAILED_PRECONDITION: |
| 49 | + return "FAILED_PRECONDITION"; |
| 50 | + case grpc::StatusCode::ABORTED: |
| 51 | + return "ABORTED"; |
| 52 | + case grpc::StatusCode::OUT_OF_RANGE: |
| 53 | + return "OUT_OF_RANGE"; |
| 54 | + case grpc::StatusCode::UNIMPLEMENTED: |
| 55 | + return "UNIMPLEMENTED"; |
| 56 | + case grpc::StatusCode::INTERNAL: |
| 57 | + return "INTERNAL"; |
| 58 | + case grpc::StatusCode::UNAVAILABLE: |
| 59 | + return "UNAVAILABLE"; |
| 60 | + case grpc::StatusCode::DATA_LOSS: |
| 61 | + return "DATA_LOSS"; |
| 62 | + case grpc::StatusCode::UNAUTHENTICATED: |
| 63 | + return "UNAUTHENTICATED"; |
| 64 | + default: |
| 65 | + return "invalid status code value"; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +} // namespace |
| 70 | + |
| 71 | +std::string ToString(const grpc::Status& status) { |
| 72 | + if (status.ok()) { |
| 73 | + return "OK"; |
| 74 | + } |
| 75 | + return absl::StrCat(StatusCodeName(status.error_code()), ": ", |
| 76 | + status.error_message()); |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace common |
| 80 | +} // namespace opencensus |
0 commit comments