8000 custom error · Arc-Jung/firebase-admin-python@5d81e8c · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d81e8c

Browse files
committed
custom error
1 parent 70a3340 commit 5d81e8c

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

firebase_admin/dynamic_links.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
This module lets admins get statistics for a Firebase dynamic link.
1818
"""
1919

20+
import requests
2021
from six.moves import urllib
2122
import six
2223

@@ -118,14 +119,14 @@ def __init__(self, platform, event, count):
118119
"""Create new instance of EventStats(platform, event, count)"""
119120
if isinstance(platform, six.string_types) and platform in self._platforms.keys():
120121
raise ValueError(('Raw string "{}" detected. Use a dynamic_links.PLATFORM_* constant' +
121-
' or the make_event_stat() method.').format(platform))
122+
' or the make_from_strings() method.').format(platform))
122123
if not isinstance(platform, six.string_types) or platform not in self._platforms.values():
123124
raise ValueError('platform {}, not recognized'.format(platform))
124125
self._platform = platform
125126

126127
if isinstance(event, six.string_types) and event in self._event_types.keys():
127128
raise ValueError(('Raw string {} detected. Use one of the dynamic_links.EVENT_TYPES_' +
128-
' constants, or the make_event_stat() method.').format(event))
129+
' constants, or the make_from_strings() method.').format(event))
129130
if not isinstance(event, six.string_types) or event not in self._event_types.values():
130131
raise ValueError('event_type {}, not recognized'.format(event))
131132
self._event = event
@@ -134,14 +135,10 @@ def __init__(self, platform, event, count):
134135
raise ValueError('Count: {} must be a non negative int'.format(count))
135136
self._count = count
136137

137-
def __repr__(self):
138-
return"EventStats(platform: '{}', event: '{}', count: '{}')".format(
139-
self.platform, self.event, self.count)
140-
141138
@classmethod
142-
def make_event_stat(cls, platform, event, count):
143-
"""make_event_stat creates an EventStat object given the appropriate constants. e.g:
144-
make_event_stat(platform=PLATFORM_DESKTOP, event=EVENT_TYPE_REDIRECT, count=4)"""
139+
def make_from_strings(cls, platform, event, count):
140+
"""make_from_strings creates an EventStat object given the appropriate constants. e.g:
141+
make_from_strings(platform=PLATFORM_DESKTOP, event=EVENT_TYPE_REDIRECT, count=4)"""
145142
return EventStats(cls._platforms[platform],
146143
cls._event_types[event],
147144
int(count))
@@ -178,6 +175,9 @@ def duration_days(self, duration_days):
178175

179176
class _LinksService(object):
180177
"""Provides methods for the Firebase dynamic links interaction"""
178+
179+
INTERNAL_ERROR = 'internal-error'
180+
181181
def __init__(self, app):
182182
self._client = _http_client.JsonHttpClient(
183183
credential=app.credential.get_credential(),
@@ -202,7 +202,20 @@ def get_stats(self, short_link, stat_options):
202202
raise ValueError('stat_options must be of type StatOptions.')
203203

204204
request_string = self._format_request_string(short_link, stat_options)
205-
resp = self._client.body('get', request_string)
206-
link_event_stats_dict = resp.get('linkEventStats', [])
207-
event_stats = [EventStats.make_event_stat(**es) for es in link_event_stats_dict]
205+
try:
206+
resp = self._client.body('get', request_string)
207+
link_event_stats_dict = resp.get('linkEventStats', [])
208+
except requests.exceptions.RequestException as error:
209+
msg = 'Failed to call dynamic links API: {0}'.format(error)
210+
raise ApiCallError(self.INTERNAL_ERROR, msg, error)
211+
event_stats = [EventStats.make_from_strings(**es) for es in link_event_stats_dict]
208212
return LinkStats(event_stats)
213+
214+
215+
class ApiCallError(Exception):
216+
"""Represents an Exception encountered while invoking the Firebase dynamic links API."""
217+
218+
def __init__(self, code, message, error=None):
219+
Exception.__init__(self, message)
220+
self.code = code
221+
self.detail = error

tests/test_dynamic_links.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ def test_get_stats_error(self, dynamic_links_test):
9797
dynamic_links_test._instrument_dynamic_links(payload=MOCK_GET_STATS_RESPONSE,
9898
status=500)
9999
options = dynamic_links.StatOptions(duration_days=9)
100-
with pytest.raises(requests.exceptions.HTTPError) as excinfo:
100+
with pytest.raises(dynamic_links.ApiCallError) as excinfo:
101101
dynamic_links.get_link_stats(MOCK_SHORT_URL, options, app=dynamic_links_test.app)
102-
assert excinfo.value.response.status_code == 500
102+
assert excinfo.value.code == 500
103103

104104
@pytest.mark.parametrize('invalid_url', ['google.com'] + INVALID_STRINGS)
105105
def test_get_stats_invalid_url(self, dynamic_links_test, invalid_url):

0 commit comments

Comments
 (0)
0