• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 struct status_string_entry {
26   const char* str;
27   grpc_status_code status;
28 };
29 static const status_string_entry g_status_string_entries[] = {
30     {"OK", GRPC_STATUS_OK},
31     {"CANCELLED", GRPC_STATUS_CANCELLED},
32     {"UNKNOWN", GRPC_STATUS_UNKNOWN},
33     {"INVALID_ARGUMENT", GRPC_STATUS_INVALID_ARGUMENT},
34     {"DEADLINE_EXCEEDED", GRPC_STATUS_DEADLINE_EXCEEDED},
35     {"NOT_FOUND", GRPC_STATUS_NOT_FOUND},
36     {"ALREADY_EXISTS", GRPC_STATUS_ALREADY_EXISTS},
37     {"PERMISSION_DENIED", GRPC_STATUS_PERMISSION_DENIED},
38     {"UNAUTHENTICATED", GRPC_STATUS_UNAUTHENTICATED},
39     {"RESOURCE_EXHAUSTED", GRPC_STATUS_RESOURCE_EXHAUSTED},
40     {"FAILED_PRECONDITION", GRPC_STATUS_FAILED_PRECONDITION},
41     {"ABORTED", GRPC_STATUS_ABORTED},
42     {"OUT_OF_RANGE", GRPC_STATUS_OUT_OF_RANGE},
43     {"UNIMPLEMENTED", GRPC_STATUS_UNIMPLEMENTED},
44     {"INTERNAL", GRPC_STATUS_INTERNAL},
45     {"UNAVAILABLE", GRPC_STATUS_UNAVAILABLE},
46     {"DATA_LOSS", GRPC_STATUS_DATA_LOSS},
47 };
48 
grpc_status_code_from_string(const char * status_str,grpc_status_code * status)49 bool grpc_status_code_from_string(const char* status_str,
50                                   grpc_status_code* status) {
51   for (size_t i = 0; i < GPR_ARRAY_SIZE(g_status_string_entries); ++i) {
52     if (strcmp(status_str, g_status_string_entries[i].str) == 0) {
53       *status = g_status_string_entries[i].status;
54       return true;
55     }
56   }
57   return false;
58 }
59 
grpc_status_code_to_string(grpc_status_code status)60 const char* grpc_status_code_to_string(grpc_status_code status) {
61   switch (status) {
62     case GRPC_STATUS_OK:
63       return "OK";
64     case GRPC_STATUS_CANCELLED:
65       return "CANCELLED";
66     case GRPC_STATUS_UNKNOWN:
67       return "UNKNOWN";
68     case GRPC_STATUS_INVALID_ARGUMENT:
69       return "INVALID_ARGUMENT";
70     case GRPC_STATUS_DEADLINE_EXCEEDED:
71       return "DEADLINE_EXCEEDED";
72     case GRPC_STATUS_NOT_FOUND:
73       return "NOT_FOUND";
74     case GRPC_STATUS_ALREADY_EXISTS:
75       return "ALREADY_EXISTS";
76     case GRPC_STATUS_PERMISSION_DENIED:
77       return "PERMISSION_DENIED";
78     case GRPC_STATUS_UNAUTHENTICATED:
79       return "UNAUTHENTICATED";
80     case GRPC_STATUS_RESOURCE_EXHAUSTED:
81       return "RESOURCE_EXHAUSTED";
82     case GRPC_STATUS_FAILED_PRECONDITION:
83       return "FAILED_PRECONDITION";
84     case GRPC_STATUS_ABORTED:
85       return "ABORTED";
86     case GRPC_STATUS_OUT_OF_RANGE:
87       return "OUT_OF_RANGE";
88     case GRPC_STATUS_UNIMPLEMENTED:
89       return "UNIMPLEMENTED";
90     case GRPC_STATUS_INTERNAL:
91       return "INTERNAL";
92     case GRPC_STATUS_UNAVAILABLE:
93       return "UNAVAILABLE";
94     case GRPC_STATUS_DATA_LOSS:
95       return "DATA_LOSS";
96     default:
97       return "UNKNOWN";
98   }
99 }
100