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 
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 
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 
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_INTERNAL;
72     case 401:
73       return GRPC_STATUS_UNAUTHENTICATED;
74     case 403:
75       return GRPC_STATUS_PERMISSION_DENIED;
76     case 404:
77       return GRPC_STATUS_UNIMPLEMENTED;
78     case 429:
79       return GRPC_STATUS_UNAVAILABLE;
80     case 502:
81       return GRPC_STATUS_UNAVAILABLE;
82     case 503:
83       return GRPC_STATUS_UNAVAILABLE;
84     case 504:
85       return GRPC_STATUS_UNAVAILABLE;
86     /* everything else is unknown */
87     default:
88       return GRPC_STATUS_UNKNOWN;
89   }
90 }
91 
92 int grpc_status_to_http2_status(grpc_status_code /*status*/) { return 200; }
93