Skip to content
Next Next commit
Firebase ML Modify Operation Handling Code to match actual codes
  • Loading branch information
ifielker committed Jan 23, 2020
commit fcc3b2ddd4ff2f9ded27b346bcd09be78c42c534
26 changes: 24 additions & 2 deletions firebase_admin/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@
}


_RPC_CODE_TO_ERROR_CODE = {
1: exceptions.CANCELLED,
2: exceptions.UNKNOWN,
3: exceptions.INVALID_ARGUMENT,
4: exceptions.DEADLINE_EXCEEDED,
5: exceptions.NOT_FOUND,
6: exceptions.ALREADY_EXISTS,
7: exceptions.PERMISSION_DENIED,
8: exceptions.RESOURCE_EXHAUSTED,
9: exceptions.FAILED_PRECONDITION,
10: exceptions.ABORTED,
11: exceptions.OUT_OF_RANGE,
13: exceptions.INTERNAL,
14: exceptions.UNAVAILABLE,
15: exceptions.DATA_LOSS,
16: exceptions.UNAUTHENTICATED,
}


def _get_initialized_app(app):
if app is None:
return firebase_admin.get_app()
Expand Down Expand Up @@ -120,9 +139,9 @@ def handle_operation_error(error):
message='Unknown error while making a remote service call: {0}'.format(error),
cause=error)

status_code = error.get('code')
rpc_code = error.get('code')
message = error.get('message')
error_code = _http_status_to_error_code(status_code)
error_code = _rpc_code_to_error_code(rpc_code)
err_type = _error_code_to_exception_type(error_code)
return err_type(message=message)

Expand Down Expand Up @@ -283,6 +302,9 @@ def _http_status_to_error_code(status):
"""Maps an HTTP status to a platform error code."""
return _HTTP_STATUS_TO_ERROR_CODE.get(status, exceptions.UNKNOWN)

def _rpc_code_to_error_code(rpc_code):
"""Maps an RPC code to a platform error code."""
return _RPC_CODE_TO_ERROR_CODE.get(rpc_code, exceptions.UNKNOWN)

def _error_code_to_exception_type(code):
"""Maps a platform error code to an exception type."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ml.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
# Name is required if the operation is not done.
'done': False
}
OPERATION_ERROR_CODE = 400
OPERATION_ERROR_CODE = 3
OPERATION_ERROR_MSG = "Invalid argument"
OPERATION_ERROR_EXPECTED_STATUS = 'INVALID_ARGUMENT'
OPERATION_ERROR_JSON_1 = {
Expand Down