|
| 1 | +import grpc |
| 2 | +from . import errors |
| 3 | +from momento_wire_types import cacheclient_pb2 as cache_client_types |
| 4 | + |
| 5 | +__rpc_to_error = { |
| 6 | + grpc.StatusCode.ALREADY_EXISTS: errors.CacheExistsError, |
| 7 | + grpc.StatusCode.INVALID_ARGUMENT: errors.CacheValueError, |
| 8 | + grpc.StatusCode.NOT_FOUND: errors.CacheNotFoundError, |
| 9 | + grpc.StatusCode.PERMISSION_DENIED: errors.PermissionError, |
| 10 | +} |
| 11 | + |
| 12 | +# Till the time MR2 stops returning errors in Enums |
| 13 | +__ecache_result_to_error = { |
| 14 | + cache_client_types.Bad_Request: errors.CacheValueError, |
| 15 | + cache_client_types.Internal_Server_Error: errors.InternalServerError, |
| 16 | + cache_client_types.Service_Unavailable: errors.InternalServerError, |
| 17 | + cache_client_types.Unauthorized: errors.PermissionError, |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +def convert(exception): |
| 22 | + if (isinstance(exception, errors.SdkError)): |
| 23 | + return exception |
| 24 | + |
| 25 | + if (isinstance(exception, grpc.RpcError)): |
| 26 | + if exception.code() in __rpc_to_error: |
| 27 | + return __rpc_to_error[exception.code()](exception.details()) |
| 28 | + else: |
| 29 | + return errors.InternalServerError( |
| 30 | + 'CacheService failed with an internal error') |
| 31 | + |
| 32 | + return errors.ClientSdkError('Operation failed with error: ' + |
| 33 | + str(exception)) |
| 34 | + |
| 35 | + |
| 36 | +def convert_ecache_result(ecache_result, message): |
| 37 | + if (ecache_result in __ecache_result_to_error): |
| 38 | + return __ecache_result_to_error[ecache_result](message) |
| 39 | + return errors.InternalServerError( |
| 40 | + 'CacheService failed with an internal error') |
0 commit comments