1 /*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/channel/status_util.h"
22
23 #include "src/core/lib/gpr/useful.h"
24
25 typedef struct {
26 const char* str;
27 grpc_status_code status;
28 } status_string_entry;
29
30 static const status_string_entry g_status_string_entries[] = {
31 {"OK", GRPC_STATUS_OK},
32 {"CANCELLED", GRPC_STATUS_CANCELLED},
33 {"UNKNOWN", GRPC_STATUS_UNKNOWN},
34 {"INVALID_ARGUMENT", GRPC_STATUS_INVALID_ARGUMENT},
35 {"DEADLINE_EXCEEDED", GRPC_STATUS_DEADLINE_EXCEEDED},
36 {"NOT_FOUND", GRPC_STATUS_NOT_FOUND},
37 {"ALREADY_EXISTS", GRPC_STATUS_ALREADY_EXISTS},
38 {"PERMISSION_DENIED", GRPC_STATUS_PERMISSION_DENIED},
39 {"UNAUTHENTICATED", GRPC_STATUS_UNAUTHENTICATED},
40 {"RESOURCE_EXHAUSTED", GRPC_STATUS_RESOURCE_EXHAUSTED},
41 {"FAILED_PRECONDITION", GRPC_STATUS_FAILED_PRECONDITION},
42 {"ABORTED", GRPC_STATUS_ABORTED},
43 {"OUT_OF_RANGE", GRPC_STATUS_OUT_OF_RANGE},
44 {"UNIMPLEMENTED", GRPC_STATUS_UNIMPLEMENTED},
45 {"INTERNAL", GRPC_STATUS_INTERNAL},
46 {"UNAVAILABLE", GRPC_STATUS_UNAVAILABLE},
47 {"DATA_LOSS", GRPC_STATUS_DATA_LOSS},
48 };
49
grpc_status_code_from_string(const char * status_str,grpc_status_code * status)50 bool grpc_status_code_from_string(const char* status_str,
51 grpc_status_code* status) {
52 for (size_t i = 0; i < GPR_ARRAY_SIZE(g_status_string_entries); ++i) {
53 if (strcmp(status_str, g_status_string_entries[i].str) == 0) {
54 *status = g_status_string_entries[i].status;
55 return true;
56 }
57 }
58 return false;
59 }
60
grpc_status_code_to_string(grpc_status_code status)61 const char* grpc_status_code_to_string(grpc_status_code status) {
62 switch (status) {
63 case GRPC_STATUS_OK:
64 return "OK";
65 case GRPC_STATUS_CANCELLED:
66 return "CANCELLED";
67 case GRPC_STATUS_UNKNOWN:
68 return "UNKNOWN";
69 case GRPC_STATUS_INVALID_ARGUMENT:
70 return "INVALID_ARGUMENT";
71 case GRPC_STATUS_DEADLINE_EXCEEDED:
72 return "DEADLINE_EXCEEDED";
73 case GRPC_STATUS_NOT_FOUND:
74 return "NOT_FOUND";
75 case GRPC_STATUS_ALREADY_EXISTS:
76 return "ALREADY_EXISTS";
77 case GRPC_STATUS_PERMISSION_DENIED:
78 return "PERMISSION_DENIED";
79 case GRPC_STATUS_UNAUTHENTICATED:
80 return "UNAUTHENTICATED";
81 case GRPC_STATUS_RESOURCE_EXHAUSTED:
82 return "RESOURCE_EXHAUSTED";
83 case GRPC_STATUS_FAILED_PRECONDITION:
84 return "FAILED_PRECONDITION";
85 case GRPC_STATUS_ABORTED:
86 return "ABORTED";
87 case GRPC_STATUS_OUT_OF_RANGE:
88 return "OUT_OF_RANGE";
89 case GRPC_STATUS_UNIMPLEMENTED:
90 return "UNIMPLEMENTED";
91 case GRPC_STATUS_INTERNAL:
92 return "INTERNAL";
93 case GRPC_STATUS_UNAVAILABLE:
94 return "UNAVAILABLE";
95 case GRPC_STATUS_DATA_LOSS:
96 return "DATA_LOSS";
97 default:
98 return "UNKNOWN";
99 }
100 }
101