Skip to content

Add Tracking IDs to all API Errors and Warnings #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/user/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ Exceptions
Warnings
========

.. autoexception:: webexteamssdkWarning()
:show-inheritance:
:members:

.. autoexception:: ApiWarning()
:show-inheritance:
:members:

.. autoexception:: RateLimitWarning()
:show-inheritance:
:members:
Expand Down
4 changes: 2 additions & 2 deletions webexteamssdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
from ._version import get_versions
from .api import WebexTeamsAPI
from .exceptions import (
AccessTokenError, ApiError, MalformedResponse, RateLimitError,
RateLimitWarning, webexteamssdkException,
AccessTokenError, ApiError, ApiWarning, MalformedResponse, RateLimitError,
RateLimitWarning, webexteamssdkException, webexteamssdkWarning,
)
from .models.dictionary import dict_data_factory
from .models.immutable import (
Expand Down
53 changes: 32 additions & 21 deletions webexteamssdk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class webexteamssdkException(Exception):
pass


class webexteamssdkWarning(webexteamssdkException, Warning):
"""Base class for all webexteamssdk warnings."""
pass


class AccessTokenError(webexteamssdkException):
"""Raised when an incorrect Webex Teams Access Token has been provided."""
pass
Expand Down Expand Up @@ -73,6 +78,9 @@ def __init__(self, response):
self.status = self.response.reason
"""The HTTP status from the API response."""

self.description = RESPONSE_CODES.get(self.status_code)
"""A description of the HTTP Response Code from the API docs."""

self.details = None
"""The parsed JSON details from the API response."""
if "application/json" in \
Expand All @@ -85,24 +93,40 @@ def __init__(self, response):
self.message = self.details.get("message") if self.details else None
"""The error message from the parsed API response."""

self.description = RESPONSE_CODES.get(self.status_code)
"""A description of the HTTP Response Code from the API docs."""
self.tracking_id = (
self.details.get("trackingId") if self.details else None
or self.response.headers.get("trackingId")
)
"""The Webex Tracking ID from the response."""

super(ApiError, self).__init__(
"[{status_code}]{status} - {message}".format(
self.error_message = (
"[{status_code}]{status} - {detail}{tracking_id}".format(
status_code=self.status_code,
status=" " + self.status if self.status else "",
message=self.message or self.description or "Unknown Error",
detail=self.message or self.description or "Unknown Error",
tracking_id=" [Tracking ID: " + self.tracking_id + "]"
if self.tracking_id else "",
)
)

super(ApiError, self).__init__(self.error_message)

def __repr__(self):
return "<{exception_name} [{status_code}]>".format(
return "<{exception_name} [{status_code}]{status}>".format(
exception_name=self.__class__.__name__,
status_code=self.status_code,
status=" " + self.status if self.status else "",
)


class ApiWarning(webexteamssdkWarning, ApiError):
"""Warnings raised from API responses received from the Webex APIs.

Several data attributes are available for inspection.
"""
pass


class RateLimitError(ApiError):
"""Webex Teams Rate-Limit exceeded Error.

Expand All @@ -125,26 +149,13 @@ def __init__(self, response):
super(RateLimitError, self).__init__(response)


class RateLimitWarning(UserWarning):
class RateLimitWarning(ApiWarning, RateLimitError):
"""Webex Teams rate-limit exceeded warning.

Raised when a rate-limit exceeded message is received and the request will
be retried.
"""

def __init__(self, response):
assert isinstance(response, requests.Response)

# Extended warning attributes
self.retry_after = max(1, int(response.headers.get('Retry-After', 15)))
"""The `Retry-After` time period (in seconds) provided by Webex Teams.

Defaults to 15 seconds if the response `Retry-After` header isn't
present in the response headers, and defaults to a minimum wait time of
1 second if Webex Teams returns a `Retry-After` header of 0 seconds.
"""

super(RateLimitWarning, self).__init__()
pass


class MalformedResponse(webexteamssdkException):
Expand Down