• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 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/transport/status_conversion.h"
22 
grpc_status_to_http2_error(grpc_status_code status)23 grpc_http2_error_code grpc_status_to_http2_error(grpc_status_code status) {
24   switch (status) {
25     case GRPC_STATUS_OK:
26       return GRPC_HTTP2_NO_ERROR;
27     case GRPC_STATUS_CANCELLED:
28       return GRPC_HTTP2_CANCEL;
29     case GRPC_STATUS_DEADLINE_EXCEEDED:
30       return GRPC_HTTP2_CANCEL;
31     case GRPC_STATUS_RESOURCE_EXHAUSTED:
32       return GRPC_HTTP2_ENHANCE_YOUR_CALM;
33     case GRPC_STATUS_PERMISSION_DENIED:
34       return GRPC_HTTP2_INADEQUATE_SECURITY;
35     case GRPC_STATUS_UNAVAILABLE:
36       return GRPC_HTTP2_REFUSED_STREAM;
37     default:
38       return GRPC_HTTP2_INTERNAL_ERROR;
39   }
40 }
41 
grpc_http2_error_to_grpc_status(grpc_http2_error_code error,grpc_millis deadline)42 grpc_status_code grpc_http2_error_to_grpc_status(grpc_http2_error_code error,
43                                                  grpc_millis deadline) {
44   switch (error) {
45     case GRPC_HTTP2_NO_ERROR:
46       /* should never be received */
47       return GRPC_STATUS_INTERNAL;
48     case GRPC_HTTP2_CANCEL:
49       /* http2 cancel translates to STATUS_CANCELLED iff deadline hasn't been
50        * exceeded */
51       return grpc_core::ExecCtx::Get()->Now() > deadline
52                  ? GRPC_STATUS_DEADLINE_EXCEEDED
53                  : GRPC_STATUS_CANCELLED;
54     case GRPC_HTTP2_ENHANCE_YOUR_CALM:
55       return GRPC_STATUS_RESOURCE_EXHAUSTED;
56     case GRPC_HTTP2_INADEQUATE_SECURITY:
57       return GRPC_STATUS_PERMISSION_DENIED;
58     case GRPC_HTTP2_REFUSED_STREAM:
59       return GRPC_STATUS_UNAVAILABLE;
60     default:
61       return GRPC_STATUS_INTERNAL;
62   }
63 }
64 
grpc_http2_status_to_grpc_status(int status)65 grpc_status_code grpc_http2_status_to_grpc_status(int status) {
66   switch (status) {
67     /* these HTTP2 status codes are called out explicitly in status.proto */
68     case 200:
69       return GRPC_STATUS_OK;
70     case 400:
71       return GRPC_STATUS_INVALID_ARGUMENT;
72     case 401:
73       return GRPC_STATUS_UNAUTHENTICATED;
74     case 403:
75       return GRPC_STATUS_PERMISSION_DENIED;
76     case 404:
77       return GRPC_STATUS_NOT_FOUND;
78     case 409:
79       return GRPC_STATUS_ABORTED;
80     case 412:
81       return GRPC_STATUS_FAILED_PRECONDITION;
82     case 429:
83       return GRPC_STATUS_RESOURCE_EXHAUSTED;
84     case 499:
85       return GRPC_STATUS_CANCELLED;
86     case 500:
87       return GRPC_STATUS_UNKNOWN;
88     case 501:
89       return GRPC_STATUS_UNIMPLEMENTED;
90     case 503:
91       return GRPC_STATUS_UNAVAILABLE;
92     case 504:
93       return GRPC_STATUS_DEADLINE_EXCEEDED;
94     /* everything else is unknown */
95     default:
96       return GRPC_STATUS_UNKNOWN;
97   }
98 }
99 
grpc_status_to_http2_status(grpc_status_code status)100 int grpc_status_to_http2_status(grpc_status_code status) { return 200; }
101