Skip to content

Commit fce3be3

Browse files
authored
Merge pull request #60 from Aspire1Inspire2/Auto-refresh-query-limit
Add more exceptions to better handle error code
2 parents 096f047 + 845e8af commit fce3be3

2 files changed

Lines changed: 122 additions & 16 deletions

File tree

td/client.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,8 @@
2020
from td.app.auth import FlaskTDAuth
2121
from td.oauth import run
2222
from td.oauth import shutdown
23-
24-
class TknExpError(Exception):
25-
"""Raise exception when refresh or access token is expired.
26-
27-
Args:
28-
Exception (Exception): The base python exception class
29-
"""
30-
def __init__(self, message):
31-
"""Print out message for this exception.
32-
33-
Args:
34-
message (str): Pass in the message returned by the server.
35-
"""
36-
self.message = message
37-
super().__init__(self.message)
23+
from td.exceptions import TknExpError, ExdLmtError, NotNulError, \
24+
ForbidError, NotFndError, ServerError, GeneralError
3825

3926
class TDClient():
4027

@@ -583,8 +570,20 @@ def _make_request(self, method: str, endpoint: str, mode: str = None, params: di
583570
print("RESPONSE TEXT: {text}".format(text=response.text))
584571
print('-'*80)
585572

586-
if response.status_code == 401:
573+
if response.status_code == 400:
574+
raise NotNulError(message=response.text)
575+
elif response.status_code == 401:
587576
raise TknExpError(message=response.text)
577+
elif response.status_code == 403:
578+
raise ForbidError(message=response.text)
579+
elif response.status_code == 404:
580+
raise NotFndError(message=response.text)
581+
elif response.status_code == 429:
582+
raise ExdLmtError(message=response.text)
583+
elif response.status_code == 500 or response.status_code == 503:
584+
raise ServerError(message=response.text)
585+
elif response.status_code > 400:
586+
raise GeneralError(message=response.text)
588587

589588
def _validate_arguments(self, endpoint: str, parameter_name: str, parameter_argument: List[str]) -> bool:
590589
"""Validates arguments for an API call.

td/exceptions.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
class TknExpError(Exception):
2+
"""Raise exception when refresh or access token is expired.
3+
4+
Args:
5+
Exception (Exception): The base python exception class
6+
"""
7+
def __init__(self, message):
8+
"""Print out message for this exception.
9+
10+
Args:
11+
message (str): Pass in the message returned by the server.
12+
"""
13+
self.message = message
14+
super().__init__(self.message)
15+
16+
class ExdLmtError(Exception):
17+
"""Raise exception when exceeding query limit of the server.
18+
19+
Args:
20+
Exception (Exception): The base python exception class
21+
"""
22+
def __init__(self, message):
23+
"""Print out message for this exception.
24+
25+
Args:
26+
message (str): Pass in the message returned by the server.
27+
"""
28+
self.message = message
29+
super().__init__(self.message)
30+
31+
class NotNulError(Exception):
32+
"""Raise exception when a null value is passed into non-null field.
33+
34+
Args:
35+
Exception (Exception): The base python exception class
36+
"""
37+
def __init__(self, message):
38+
"""Print out message for this exception.
39+
40+
Args:
41+
message (str): Pass in the message returned by the server.
42+
"""
43+
self.message = message
44+
super().__init__(self.message)
45+
46+
class ForbidError(Exception):
47+
"""Raise forbidden exception. This usually occurs when the app does
48+
not have access to the account.
49+
50+
Args:
51+
Exception (Exception): The base python exception class
52+
"""
53+
def __init__(self, message):
54+
"""Print out message for this exception.
55+
56+
Args:
57+
message (str): Pass in the message returned by the server.
58+
"""
59+
self.message = message
60+
super().__init__(self.message)
61+
62+
class NotFndError(Exception):
63+
"""Raise exception when criteria is not found.
64+
65+
Args:
66+
Exception (Exception): The base python exception class
67+
"""
68+
def __init__(self, message):
69+
"""Print out message for this exception.
70+
71+
Args:
72+
message (str): Pass in the message returned by the server.
73+
"""
74+
self.message = message
75+
super().__init__(self.message)
76+
77+
class ServerError(Exception):
78+
"""Raise exception when there is an error with the service or the server
79+
cannot provide response.
80+
81+
Args:
82+
Exception (Exception): The base python exception class
83+
"""
84+
def __init__(self, message):
85+
"""Print out message for this exception.
86+
87+
Args:
88+
message (str): Pass in the message returned by the server.
89+
"""
90+
self.message = message
91+
super().__init__(self.message)
92+
93+
class GeneralError(Exception):
94+
"""Raise exception for all other status code >400 errors which are not
95+
defined above.
96+
97+
Args:
98+
Exception (Exception): The base python exception class
99+
"""
100+
def __init__(self, message):
101+
"""Print out message for this exception.
102+
103+
Args:
104+
message (str): Pass in the message returned by the server.
105+
"""
106+
self.message = message
107+
super().__init__(self.message)

0 commit comments

Comments
 (0)