|
58 | 58 | 503: exceptions.UNAVAILABLE, |
59 | 59 | } |
60 | 60 |
|
| 61 | +# See https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto |
| 62 | +_RPC_CODE_TO_ERROR_CODE = { |
| 63 | + 1: exceptions.CANCELLED, |
| 64 | + 2: exceptions.UNKNOWN, |
| 65 | + 3: exceptions.INVALID_ARGUMENT, |
| 66 | + 4: exceptions.DEADLINE_EXCEEDED, |
| 67 | + 5: exceptions.NOT_FOUND, |
| 68 | + 6: exceptions.ALREADY_EXISTS, |
| 69 | + 7: exceptions.PERMISSION_DENIED, |
| 70 | + 8: exceptions.RESOURCE_EXHAUSTED, |
| 71 | + 9: exceptions.FAILED_PRECONDITION, |
| 72 | + 10: exceptions.ABORTED, |
| 73 | + 11: exceptions.OUT_OF_RANGE, |
| 74 | + 13: exceptions.INTERNAL, |
| 75 | + 14: exceptions.UNAVAILABLE, |
| 76 | + 15: exceptions.DATA_LOSS, |
| 77 | + 16: exceptions.UNAUTHENTICATED, |
| 78 | +} |
| 79 | + |
61 | 80 |
|
62 | 81 | def _get_initialized_app(app): |
63 | 82 | if app is None: |
@@ -120,9 +139,9 @@ def handle_operation_error(error): |
120 | 139 | message='Unknown error while making a remote service call: {0}'.format(error), |
121 | 140 | cause=error) |
122 | 141 |
|
123 | | - status_code = error.get('code') |
| 142 | + rpc_code = error.get('code') |
124 | 143 | message = error.get('message') |
125 | | - error_code = _http_status_to_error_code(status_code) |
| 144 | + error_code = _rpc_code_to_error_code(rpc_code) |
126 | 145 | err_type = _error_code_to_exception_type(error_code) |
127 | 146 | return err_type(message=message) |
128 | 147 |
|
@@ -283,6 +302,9 @@ def _http_status_to_error_code(status): |
283 | 302 | """Maps an HTTP status to a platform error code.""" |
284 | 303 | return _HTTP_STATUS_TO_ERROR_CODE.get(status, exceptions.UNKNOWN) |
285 | 304 |
|
| 305 | +def _rpc_code_to_error_code(rpc_code): |
| 306 | + """Maps an RPC code to a platform error code.""" |
| 307 | + return _RPC_CODE_TO_ERROR_CODE.get(rpc_code, exceptions.UNKNOWN) |
286 | 308 |
|
287 | 309 | def _error_code_to_exception_type(code): |
288 | 310 | """Maps a platform error code to an exception type.""" |
|
0 commit comments