Skip to content

Commit b133bbb

Browse files
authored
Firebase ML Modify Operation Handling Code to match rpc codes not html codes (#390)
* Firebase ML Modify Operation Handling Code to match actual codes * apply database fix too
1 parent a13d2a7 commit b133bbb

File tree

6 files changed

+30
-8
lines changed

6 files changed

+30
-8
lines changed

firebase_admin/_utils.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@
5858
503: exceptions.UNAVAILABLE,
5959
}
6060

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+
6180

6281
def _get_initialized_app(app):
6382
if app is None:
@@ -120,9 +139,9 @@ def handle_operation_error(error):
120139
message='Unknown error while making a remote service call: {0}'.format(error),
121140
cause=error)
122141

123-
status_code = error.get('code')
142+
rpc_code = error.get('code')
124143
message = error.get('message')
125-
error_code = _http_status_to_error_code(status_code)
144+
error_code = _rpc_code_to_error_code(rpc_code)
126145
err_type = _error_code_to_exception_type(error_code)
127146
return err_type(message=message)
128147

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

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)
286308

287309
def _error_code_to_exception_type(code):
288310
"""Maps a platform error code to an exception type."""

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pytest-cov >= 2.4.0
44
pytest-localserver >= 0.4.1
55
tox >= 3.6.0
66

7-
cachecontrol >= 0.12.4
7+
cachecontrol >= 0.12.6
88
google-api-core[grpc] >= 1.7.0, < 2.0.0dev; platform.python_implementation != 'PyPy'
99
google-api-python-client >= 1.7.8
1010
google-cloud-firestore >= 0.31.0; platform.python_implementation != 'PyPy'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
long_description = ('The Firebase Admin Python SDK enables server-side (backend) Python developers '
3838
'to integrate Firebase into their services and applications.')
3939
install_requires = [
40-
'cachecontrol>=0.12.4',
40+
'cachecontrol>=0.12.6',
4141
'google-api-core[grpc] >= 1.7.0, < 2.0.0dev; platform.python_implementation != "PyPy"',
4242
'google-api-python-client >= 1.7.8',
4343
'google-cloud-firestore>=0.31.0; platform.python_implementation != "PyPy"',

tests/test_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ def test_http_timeout(self):
819819
assert ref._client.timeout == 60
820820
assert ref.get() == {}
821821
assert len(recorder) == 1
822-
assert recorder[0]._extra_kwargs['timeout'] == 60
822+
assert recorder[0]._extra_kwargs['timeout'] == pytest.approx(60, 0.001)
823823

824824
def test_app_delete(self):
825825
app = firebase_admin.initialize_app(

tests/test_messaging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ def test_send(self):
12541254
msg = messaging.Message(topic='foo')
12551255
messaging.send(msg)
12561256
assert len(self.recorder) == 1
1257-
assert self.recorder[0]._extra_kwargs['timeout'] == 4
1257+
assert self.recorder[0]._extra_kwargs['timeout'] == pytest.approx(4, 0.001)
12581258

12591259
def test_topic_management_timeout(self):
12601260
self.fcm_service._client.session.mount(
@@ -1266,7 +1266,7 @@ def test_topic_management_timeout(self):
12661266
)
12671267
messaging.subscribe_to_topic(['1'], 'a')
12681268
assert len(self.recorder) == 1
1269-
assert self.recorder[0]._extra_kwargs['timeout'] == 4
1269+
assert self.recorder[0]._extra_kwargs['timeout'] == pytest.approx(4, 0.001)
12701270

12711271

12721272
class TestSend(object):

tests/test_ml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
# Name is required if the operation is not done.
170170
'done': False
171171
}
172-
OPERATION_ERROR_CODE = 400
172+
OPERATION_ERROR_CODE = 3
173173
OPERATION_ERROR_MSG = "Invalid argument"
174174
OPERATION_ERROR_EXPECTED_STATUS = 'INVALID_ARGUMENT'
175175
OPERATION_ERROR_JSON_1 = {

0 commit comments

Comments
 (0)