From 9a0903aac7dae37b7f1ea405d1ce5a10f73473ae Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Wed, 26 Feb 2020 11:01:18 -0800 Subject: [PATCH 1/3] fix(fcm): Updated topic management error format --- firebase_admin/messaging.py | 11 +++++++---- tests/test_messaging.py | 8 ++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/firebase_admin/messaging.py b/firebase_admin/messaging.py index e4e223091..c99d020fa 100644 --- a/firebase_admin/messaging.py +++ b/firebase_admin/messaging.py @@ -453,10 +453,13 @@ def _handle_iid_error(self, error): except ValueError: pass - # IID error response format: {"error": "some error message"} - msg = data.get('error') - if not msg: - msg = 'Unexpected HTTP response with status: {0}; body: {1}'.format( + # IID error response format: {"error": "ErrorCode"} + msg = 'Error while calling the IID service' + code = data.get('error') + if code: + msg += ' ({0})'.format(code) + else: + msg += '; status: {0}; body: {1}'.format( error.response.status_code, error.response.content.decode()) return _utils.handle_requests_error(error, msg) diff --git a/tests/test_messaging.py b/tests/test_messaging.py index 6e776cc5f..a1b2d79fb 100644 --- a/tests/test_messaging.py +++ b/tests/test_messaging.py @@ -2286,7 +2286,7 @@ def test_subscribe_to_topic_error(self, status, exc_type): status=status, payload=self._DEFAULT_ERROR_RESPONSE) with pytest.raises(exc_type) as excinfo: messaging.subscribe_to_topic('foo', 'test-topic') - assert str(excinfo.value) == 'error_reason' + assert str(excinfo.value) == 'Error while calling the IID service (error_reason)' assert len(recorder) == 1 assert recorder[0].method == 'POST' assert recorder[0].url == self._get_url('iid/v1:batchAdd') @@ -2296,7 +2296,7 @@ def test_subscribe_to_topic_non_json_error(self, status, exc_type): _, recorder = self._instrument_iid_service(status=status, payload='not json') with pytest.raises(exc_type) as excinfo: messaging.subscribe_to_topic('foo', 'test-topic') - reason = 'Unexpected HTTP response with status: {0}; body: not json'.format(status) + reason = 'Error while calling the IID service; status: {0}; body: not json'.format(status) assert str(excinfo.value) == reason assert len(recorder) == 1 assert recorder[0].method == 'POST' @@ -2318,7 +2318,7 @@ def test_unsubscribe_from_topic_error(self, status, exc_type): status=status, payload=self._DEFAULT_ERROR_RESPONSE) with pytest.raises(exc_type) as excinfo: messaging.unsubscribe_from_topic('foo', 'test-topic') - assert str(excinfo.value) == 'error_reason' + assert str(excinfo.value) == 'Error while calling the IID service (error_reason)' assert len(recorder) == 1 assert recorder[0].method == 'POST' assert recorder[0].url == self._get_url('iid/v1:batchRemove') @@ -2328,7 +2328,7 @@ def test_unsubscribe_from_topic_non_json_error(self, status, exc_type): _, recorder = self._instrument_iid_service(status=status, payload='not json') with pytest.raises(exc_type) as excinfo: messaging.unsubscribe_from_topic('foo', 'test-topic') - reason = 'Unexpected HTTP response with status: {0}; body: not json'.format(status) + reason = 'Error while calling the IID service; status: {0}; body: not json'.format(status) assert str(excinfo.value) == reason assert len(recorder) == 1 assert recorder[0].method == 'POST' From 52509ed51793be1db76837b375dbfb3cf89b0085 Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Wed, 26 Feb 2020 17:07:23 -0800 Subject: [PATCH 2/3] Better default error messages --- firebase_admin/messaging.py | 4 ++-- tests/test_messaging.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/firebase_admin/messaging.py b/firebase_admin/messaging.py index c99d020fa..46d18920f 100644 --- a/firebase_admin/messaging.py +++ b/firebase_admin/messaging.py @@ -457,9 +457,9 @@ def _handle_iid_error(self, error): msg = 'Error while calling the IID service' code = data.get('error') if code: - msg += ' ({0})'.format(code) + msg = 'Error while calling the IID service: {0}'.format(code) else: - msg += '; status: {0}; body: {1}'.format( + msg = 'Unexpected HTTP response with status: {0}; body: {1}'.format( error.response.status_code, error.response.content.decode()) return _utils.handle_requests_error(error, msg) diff --git a/tests/test_messaging.py b/tests/test_messaging.py index a1b2d79fb..6333aad46 100644 --- a/tests/test_messaging.py +++ b/tests/test_messaging.py @@ -2286,7 +2286,7 @@ def test_subscribe_to_topic_error(self, status, exc_type): status=status, payload=self._DEFAULT_ERROR_RESPONSE) with pytest.raises(exc_type) as excinfo: messaging.subscribe_to_topic('foo', 'test-topic') - assert str(excinfo.value) == 'Error while calling the IID service (error_reason)' + assert str(excinfo.value) == 'Error while calling the IID service: error_reason' assert len(recorder) == 1 assert recorder[0].method == 'POST' assert recorder[0].url == self._get_url('iid/v1:batchAdd') @@ -2296,7 +2296,7 @@ def test_subscribe_to_topic_non_json_error(self, status, exc_type): _, recorder = self._instrument_iid_service(status=status, payload='not json') with pytest.raises(exc_type) as excinfo: messaging.subscribe_to_topic('foo', 'test-topic') - reason = 'Error while calling the IID service; status: {0}; body: not json'.format(status) + reason = 'Unexpected HTTP response with status: {0}; body: not json'.format(status) assert str(excinfo.value) == reason assert len(recorder) == 1 assert recorder[0].method == 'POST' @@ -2318,7 +2318,7 @@ def test_unsubscribe_from_topic_error(self, status, exc_type): status=status, payload=self._DEFAULT_ERROR_RESPONSE) with pytest.raises(exc_type) as excinfo: messaging.unsubscribe_from_topic('foo', 'test-topic') - assert str(excinfo.value) == 'Error while calling the IID service (error_reason)' + assert str(excinfo.value) == 'Error while calling the IID service: error_reason' assert len(recorder) == 1 assert recorder[0].method == 'POST' assert recorder[0].url == self._get_url('iid/v1:batchRemove') @@ -2328,7 +2328,7 @@ def test_unsubscribe_from_topic_non_json_error(self, status, exc_type): _, recorder = self._instrument_iid_service(status=status, payload='not json') with pytest.raises(exc_type) as excinfo: messaging.unsubscribe_from_topic('foo', 'test-topic') - reason = 'Error while calling the IID service; status: {0}; body: not json'.format(status) + reason = 'Unexpected HTTP response with status: {0}; body: not json'.format(status) assert str(excinfo.value) == reason assert len(recorder) == 1 assert recorder[0].method == 'POST' From c7325127ebd5ff93f8a233a60aa3c4bd932aa4f3 Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Thu, 27 Feb 2020 11:19:17 -0800 Subject: [PATCH 3/3] Removed redundant variable initializer --- firebase_admin/messaging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase_admin/messaging.py b/firebase_admin/messaging.py index 46d18920f..217cf0a56 100644 --- a/firebase_admin/messaging.py +++ b/firebase_admin/messaging.py @@ -454,8 +454,8 @@ def _handle_iid_error(self, error): pass # IID error response format: {"error": "ErrorCode"} - msg = 'Error while calling the IID service' code = data.get('error') + msg = None if code: msg = 'Error while calling the IID service: {0}'.format(code) else: