Skip to content

Commit 5588f34

Browse files
committed
Add Tracking ID to API exceptions and warnings
Add the `trackingId` returned in Webex API responses to the library API errors and warnings. Clean-up exception and warning inheritance to reduce code duplication and provide a consistent inheritance hierarchy.
1 parent 704904a commit 5588f34

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

docs/user/api.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ Exceptions
324324
Warnings
325325
========
326326

327+
.. autoexception:: webexteamssdkWarning()
328+
:show-inheritance:
329+
:members:
330+
331+
.. autoexception:: ApiWarning()
332+
:show-inheritance:
333+
:members:
334+
327335
.. autoexception:: RateLimitWarning()
328336
:show-inheritance:
329337
:members:

webexteamssdk/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
from ._version import get_versions
3838
from .api import WebexTeamsAPI
3939
from .exceptions import (
40-
AccessTokenError, ApiError, MalformedResponse, RateLimitError,
41-
RateLimitWarning, webexteamssdkException,
40+
AccessTokenError, ApiError, ApiWarning, MalformedResponse, RateLimitError,
41+
RateLimitWarning, webexteamssdkException, webexteamssdkWarning,
4242
)
4343
from .models.dictionary import dict_data_factory
4444
from .models.immutable import (

webexteamssdk/exceptions.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class webexteamssdkException(Exception):
4646
pass
4747

4848

49+
class webexteamssdkWarning(webexteamssdkException, Warning):
50+
"""Base class for all webexteamssdk warnings."""
51+
pass
52+
53+
4954
class AccessTokenError(webexteamssdkException):
5055
"""Raised when an incorrect Webex Teams Access Token has been provided."""
5156
pass
@@ -73,6 +78,9 @@ def __init__(self, response):
7378
self.status = self.response.reason
7479
"""The HTTP status from the API response."""
7580

81+
self.description = RESPONSE_CODES.get(self.status_code)
82+
"""A description of the HTTP Response Code from the API docs."""
83+
7684
self.details = None
7785
"""The parsed JSON details from the API response."""
7886
if "application/json" in \
@@ -85,24 +93,40 @@ def __init__(self, response):
8593
self.message = self.details.get("message") if self.details else None
8694
"""The error message from the parsed API response."""
8795

88-
self.description = RESPONSE_CODES.get(self.status_code)
89-
"""A description of the HTTP Response Code from the API docs."""
96+
self.tracking_id = (
97+
self.details.get("trackingId") if self.details else None
98+
or self.response.headers.get("trackingId")
99+
)
100+
"""The Webex Tracking ID from the response."""
90101

91-
super(ApiError, self).__init__(
92-
"[{status_code}]{status} - {message}".format(
102+
self.error_message = (
103+
"[{status_code}]{status} - {detail}{tracking_id}".format(
93104
status_code=self.status_code,
94105
status=" " + self.status if self.status else "",
95-
message=self.message or self.description or "Unknown Error",
106+
detail=self.message or self.description or "Unknown Error",
107+
tracking_id=" [Tracking ID: " + self.tracking_id + "]"
108+
if self.tracking_id else "",
96109
)
97110
)
98111

112+
super(ApiError, self).__init__(self.error_message)
113+
99114
def __repr__(self):
100-
return "<{exception_name} [{status_code}]>".format(
115+
return "<{exception_name} [{status_code}]{status}>".format(
101116
exception_name=self.__class__.__name__,
102117
status_code=self.status_code,
118+
status=" " + self.status if self.status else "",
103119
)
104120

105121

122+
class ApiWarning(webexteamssdkWarning, ApiError):
123+
"""Warnings raised from API responses received from the Webex APIs.
124+
125+
Several data attributes are available for inspection.
126+
"""
127+
pass
128+
129+
106130
class RateLimitError(ApiError):
107131
"""Webex Teams Rate-Limit exceeded Error.
108132
@@ -125,26 +149,13 @@ def __init__(self, response):
125149
super(RateLimitError, self).__init__(response)
126150

127151

128-
class RateLimitWarning(UserWarning):
152+
class RateLimitWarning(ApiWarning, RateLimitError):
129153
"""Webex Teams rate-limit exceeded warning.
130154
131155
Raised when a rate-limit exceeded message is received and the request will
132156
be retried.
133157
"""
134-
135-
def __init__(self, response):
136-
assert isinstance(response, requests.Response)
137-
138-
# Extended warning attributes
139-
self.retry_after = max(1, int(response.headers.get('Retry-After', 15)))
140-
"""The `Retry-After` time period (in seconds) provided by Webex Teams.
141-
142-
Defaults to 15 seconds if the response `Retry-After` header isn't
143-
present in the response headers, and defaults to a minimum wait time of
144-
1 second if Webex Teams returns a `Retry-After` header of 0 seconds.
145-
"""
146-
147-
super(RateLimitWarning, self).__init__()
158+
pass
148159

149160

150161
class MalformedResponse(webexteamssdkException):

0 commit comments

Comments
 (0)