Line data Source code
1 : #include "source/common/grpc/status.h" 2 : 3 : namespace Envoy { 4 : namespace Grpc { 5 : 6 4 : Status::GrpcStatus Utility::httpToGrpcStatus(uint64_t http_response_status) { 7 : // From 8 : // https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md. 9 4 : switch (http_response_status) { 10 4 : case 400: 11 4 : return Status::WellKnownGrpcStatus::Internal; 12 0 : case 401: 13 0 : return Status::WellKnownGrpcStatus::Unauthenticated; 14 0 : case 403: 15 0 : return Status::WellKnownGrpcStatus::PermissionDenied; 16 0 : case 404: 17 0 : return Status::WellKnownGrpcStatus::Unimplemented; 18 0 : case 429: 19 0 : case 502: 20 0 : case 503: 21 0 : case 504: 22 0 : return Status::WellKnownGrpcStatus::Unavailable; 23 0 : default: 24 0 : return Status::WellKnownGrpcStatus::Unknown; 25 4 : } 26 4 : } 27 : 28 25 : uint64_t Utility::grpcToHttpStatus(Status::GrpcStatus grpc_status) { 29 : // From https://cloud.google.com/apis/design/errors#handling_errors. 30 25 : switch (grpc_status) { 31 25 : case Status::WellKnownGrpcStatus::Ok: 32 25 : return 200; 33 0 : case Status::WellKnownGrpcStatus::Canceled: 34 : // Client closed request. 35 0 : return 499; 36 0 : case Status::WellKnownGrpcStatus::Unknown: 37 : // Internal server error. 38 0 : return 500; 39 0 : case Status::WellKnownGrpcStatus::InvalidArgument: 40 : // Bad request. 41 0 : return 400; 42 0 : case Status::WellKnownGrpcStatus::DeadlineExceeded: 43 : // Gateway Time-out. 44 0 : return 504; 45 0 : case Status::WellKnownGrpcStatus::NotFound: 46 : // Not found. 47 0 : return 404; 48 0 : case Status::WellKnownGrpcStatus::AlreadyExists: 49 : // Conflict. 50 0 : return 409; 51 0 : case Status::WellKnownGrpcStatus::PermissionDenied: 52 : // Forbidden. 53 0 : return 403; 54 0 : case Status::WellKnownGrpcStatus::ResourceExhausted: 55 : // Too many requests. 56 0 : return 429; 57 0 : case Status::WellKnownGrpcStatus::FailedPrecondition: 58 : // Bad request. 59 0 : return 400; 60 0 : case Status::WellKnownGrpcStatus::Aborted: 61 : // Conflict. 62 0 : return 409; 63 0 : case Status::WellKnownGrpcStatus::OutOfRange: 64 : // Bad request. 65 0 : return 400; 66 0 : case Status::WellKnownGrpcStatus::Unimplemented: 67 : // Not implemented. 68 0 : return 501; 69 0 : case Status::WellKnownGrpcStatus::Internal: 70 : // Internal server error. 71 0 : return 500; 72 0 : case Status::WellKnownGrpcStatus::Unavailable: 73 : // Service unavailable. 74 0 : return 503; 75 0 : case Status::WellKnownGrpcStatus::DataLoss: 76 : // Internal server error. 77 0 : return 500; 78 0 : case Status::WellKnownGrpcStatus::Unauthenticated: 79 : // Unauthorized. 80 0 : return 401; 81 0 : case Status::WellKnownGrpcStatus::InvalidCode: 82 0 : default: 83 : // Internal server error. 84 0 : return 500; 85 25 : } 86 25 : } 87 : 88 0 : std::string Utility::grpcStatusToString(Status::GrpcStatus grpc_status) { 89 0 : switch (grpc_status) { 90 0 : case Status::WellKnownGrpcStatus::Ok: 91 0 : return "OK"; 92 0 : case Status::WellKnownGrpcStatus::Canceled: 93 0 : return "Canceled"; 94 0 : case Status::WellKnownGrpcStatus::Unknown: 95 0 : return "Unknown"; 96 0 : case Status::WellKnownGrpcStatus::InvalidArgument: 97 0 : return "InvalidArgument"; 98 0 : case Status::WellKnownGrpcStatus::DeadlineExceeded: 99 0 : return "DeadlineExceeded"; 100 0 : case Status::WellKnownGrpcStatus::NotFound: 101 0 : return "NotFound"; 102 0 : case Status::WellKnownGrpcStatus::AlreadyExists: 103 0 : return "AlreadyExists"; 104 0 : case Status::WellKnownGrpcStatus::PermissionDenied: 105 0 : return "PermissionDenied"; 106 0 : case Status::WellKnownGrpcStatus::ResourceExhausted: 107 0 : return "ResourceExhausted"; 108 0 : case Status::WellKnownGrpcStatus::FailedPrecondition: 109 0 : return "FailedPrecondition"; 110 0 : case Status::WellKnownGrpcStatus::Aborted: 111 0 : return "Aborted"; 112 0 : case Status::WellKnownGrpcStatus::OutOfRange: 113 0 : return "OutOfRange"; 114 0 : case Status::WellKnownGrpcStatus::Unimplemented: 115 0 : return "Unimplemented"; 116 0 : case Status::WellKnownGrpcStatus::Internal: 117 0 : return "Internal"; 118 0 : case Status::WellKnownGrpcStatus::Unavailable: 119 0 : return "Unavailable"; 120 0 : case Status::WellKnownGrpcStatus::DataLoss: 121 0 : return "DataLoss"; 122 0 : case Status::WellKnownGrpcStatus::Unauthenticated: 123 0 : return "Unauthenticated"; 124 0 : case Status::WellKnownGrpcStatus::InvalidCode: 125 0 : default: 126 0 : return "InvalidCode"; 127 0 : } 128 0 : } 129 : 130 : } // namespace Grpc 131 : } // namespace Envoy