1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file |
3 | // distributed with this work for additional information |
4 | // regarding copyright ownership. The ASF licenses this file |
5 | // to you under the Apache License, Version 2.0 (the |
6 | // "License"); you may not use this file except in compliance |
7 | // with the License. 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, |
12 | // software distributed under the License is distributed on an |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | // KIND, either express or implied. See the License for the |
15 | // specific language governing permissions and limitations |
16 | // under the License. |
17 | |
18 | |
19 | #ifndef BRPC_GRPC_H |
20 | #define BRPC_GRPC_H |
21 | |
22 | #include <map> |
23 | #include <brpc/http2.h> |
24 | |
25 | namespace brpc { |
26 | |
27 | enum GrpcStatus { |
28 | // OK is returned on success. |
29 | GRPC_OK = 0, |
30 | |
31 | // CANCELED indicates the operation was canceled (typically by the caller). |
32 | GRPC_CANCELED, |
33 | |
34 | // Unknown error. An example of where this error may be returned is |
35 | // if a Status value received from another address space belongs to |
36 | // an error-space that is not known in this address space. Also |
37 | // errors raised by APIs that do not return enough error information |
38 | // may be converted to this error. |
39 | GRPC_UNKNOWN, |
40 | |
41 | // INVALIDARGUMENT Indicates client specified an invalid argument. |
42 | // Note that this differs from FAILEDPRECONDITION. It indicates arguments |
43 | // that are problematic regardless of the state of the system |
44 | // (e.g., a malformed file name). |
45 | GRPC_INVALIDARGUMENT, |
46 | |
47 | // DEADLINEEXCEEDED Means operation expired before completion. |
48 | // For operations that change the state of the system, this error may be |
49 | // returned even if the operation has completed successfully. For |
50 | // example, a successful response from a server could have been delayed |
51 | // long enough for the deadline to expire. |
52 | GRPC_DEADLINEEXCEEDED, |
53 | |
54 | // NOTFOUND Means some requested entity (e.g., file or directory) was |
55 | // not found. |
56 | GRPC_NOTFOUND, |
57 | |
58 | // ALREADYEXISTS Means an attempt to create an entity failed because one |
59 | // already exists. |
60 | GRPC_ALREADYEXISTS, |
61 | |
62 | // PERMISSIONDENIED Indicates the caller does not have permission to |
63 | // execute the specified operation. It must not be used for rejections |
64 | // caused by exhausting some resource (use ResourceExhausted |
65 | // instead for those errors). It must not be |
66 | // used if the caller cannot be identified (use UNAUTHENTICATED |
67 | // instead for those errors). |
68 | GRPC_PERMISSIONDENIED, |
69 | |
70 | // RESOURCEEXHAUSTED Indicates some resource has been exhausted, perhaps |
71 | // a per-user quota, or perhaps the entire file system is out of space. |
72 | GRPC_RESOURCEEXHAUSTED, |
73 | |
74 | // FAILEDPRECONDITION indicates operation was rejected because the |
75 | // system is not in a state required for the operation's execution. |
76 | // For example, directory to be deleted may be non-empty, an rmdir |
77 | // operation is applied to a non-directory, etc. |
78 | // |
79 | // A litmus test that may help a service implementor in deciding |
80 | // between FAILEDPRECONDITION, Aborted, and Unavailable: |
81 | // (a) Use Unavailable if the client can retry just the failing call. |
82 | // (b) Use Aborted if the client should retry at a higher-level |
83 | // (e.g., restarting a read-modify-write sequence). |
84 | // (c) Use FAILEDPRECONDITION if the client should not retry until |
85 | // the system state has been explicitly fixed. E.g., if an "rmdir" |
86 | // fails because the directory is non-empty, FAILEDPRECONDITION |
87 | // should be returned since the client should not retry unless |
88 | // they have first fixed up the directory by deleting files from it. |
89 | // (d) Use FAILEDPRECONDITION if the client performs conditional |
90 | // REST Get/Update/Delete on a resource and the resource on the |
91 | // server does not match the condition. E.g., conflicting |
92 | // read-modify-write on the same resource. |
93 | GRPC_FAILEDPRECONDITION, |
94 | |
95 | // ABORTED indicates the operation was aborted, typically due to a |
96 | // concurrency issue like sequencer check failures, transaction aborts, |
97 | // etc. |
98 | // |
99 | // See litmus test above for deciding between FAILEDPRECONDITION, |
100 | // Aborted, and Unavailable. |
101 | GPRC_ABORTED, |
102 | |
103 | // OUTOFRANGE means operation was attempted past the valid range. |
104 | // E.g., seeking or reading past end of file. |
105 | // |
106 | // Unlike INVALIDARGUMENT, this error indicates a problem that may |
107 | // be fixed if the system state changes. For example, a 32-bit file |
108 | // system will generate INVALIDARGUMENT if asked to read at an |
109 | // offset that is not in the range [0,2^32-1], but it will generate |
110 | // OUTOFRANGE if asked to read from an offset past the current |
111 | // file size. |
112 | // |
113 | // There is a fair bit of overlap between FAILEDPRECONDITION and |
114 | // OUTOFRANGE. We recommend using OUTOFRANGE (the more specific |
115 | // error) when it applies so that callers who are iterating through |
116 | // a space can easily look for an OUTOFRANGE error to detect when |
117 | // they are done. |
118 | GRPC_OUTOFRANGE, |
119 | |
120 | // UNIMPLEMENTED indicates operation is not implemented or not |
121 | // supported/enabled in this service. |
122 | GRPC_UNIMPLEMENTED, |
123 | |
124 | // INTERNAL errors. Means some invariants expected by underlying |
125 | // system has been broken. If you see one of these errors, |
126 | // something is very broken. |
127 | GRPC_INTERNAL, |
128 | |
129 | // UNAVAILABLE indicates the service is currently unavailable. |
130 | // This is a most likely a transient condition and may be corrected |
131 | // by retrying with a backoff. |
132 | // |
133 | // See litmus test above for deciding between FAILEDPRECONDITION, |
134 | // ABORTED, and UNAVAILABLE. |
135 | GRPC_UNAVAILABLE, |
136 | |
137 | // DATALOSS indicates unrecoverable data loss or corruption. |
138 | GRPC_DATALOSS, |
139 | |
140 | // UNAUTHENTICATED indicates the request does not have valid |
141 | // authentication credentials for the operation. |
142 | GRPC_UNAUTHENTICATED, |
143 | |
144 | GRPC_MAX, |
145 | }; |
146 | |
147 | // Get description of the error. |
148 | const char* GrpcStatusToString(GrpcStatus); |
149 | |
150 | // Convert between error code and grpc status with similar semantics |
151 | GrpcStatus ErrorCodeToGrpcStatus(int error_code); |
152 | int GrpcStatusToErrorCode(GrpcStatus grpc_status); |
153 | |
154 | void PercentEncode(const std::string& str, std::string* str_out); |
155 | |
156 | void PercentDecode(const std::string& str, std::string* str_out); |
157 | |
158 | |
159 | } // namespace brpc |
160 | |
161 | #endif // BRPC_GRPC_H |
162 | |