Skip to content

Commit 1285bbc

Browse files
author
Greg Taylor
committed
Improve error handling by including an error_code with each exception. Also decided on how to handle per-module error handling.
1 parent cd676f3 commit 1285bbc

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

fedex/base_service.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class FedexBaseServiceException(Exception):
1515
Serves as the base exception that other service-related exception objects
1616
are sub-classed from.
1717
"""
18-
def __init__(self, value):
18+
def __init__(self, error_code, value):
19+
self.error_code = error_code
1920
self.value = value
2021
def __str__(self):
2122
return repr(self.value)
@@ -25,8 +26,13 @@ class FedexFailure(FedexBaseServiceException):
2526
The request could not be handled at this time. This is generally a server
2627
problem.
2728
"""
28-
def __init__(self):
29-
self.value = "Your request could not be handled at this time. This is likely Fedex server problems, try again later."
29+
pass
30+
31+
class FedexError(FedexBaseServiceException):
32+
"""
33+
These are generally problems with the client-provided data.
34+
"""
35+
pass
3036

3137
class FedexBaseService(object):
3238
"""
@@ -148,13 +154,31 @@ def __check_response_for_fedex_error(self):
148154
to any one WSDL.
149155
"""
150156
if self.response.HighestSeverity == "FAILURE":
151-
raise FedexFailure()
157+
for notification in self.response.Notifications:
158+
if notification.Severity == "FAILURE":
159+
raise FedexFailure(notification.Code,
160+
notification.Message)
161+
162+
def _check_response_for_request_errors(self):
163+
"""
164+
Override this in each service module to check for errors that are
165+
specific to that module. For example, invalid tracking numbers in
166+
a Tracking request.
167+
"""
168+
pass
152169

153170
def send_request(self):
154171
"""
155172
Sends the assembled request on the child object.
156173
"""
174+
# Send the request and get the response back.
157175
self.response = self._assemble_and_send_request()
176+
# Check the response for general Fedex errors/failures that aren't
177+
# specific to any given WSDL/request.
158178
self.__check_response_for_fedex_error()
179+
# Check the response for errors specific to the particular request.
180+
# This is handled by an overridden method on the child object.
181+
self._check_response_for_request_errors()
182+
# Debug output.
159183
self.logger.info("== FEDEX QUERY RESULT ==")
160184
self.logger.info(self.response)

fedex/services/track_service.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
For more details on each, refer to the respective class's documentation.
77
"""
88
import logging
9-
from .. base_service import FedexBaseService
9+
from .. base_service import FedexBaseService, FedexError
10+
11+
class FedexInvalidTrackingNumber(FedexError):
12+
"""
13+
Sent when a bad tracking number is provided.
14+
"""
15+
pass
1016

1117
class FedexTrackRequest(FedexBaseService):
1218
"""
@@ -62,6 +68,17 @@ def __set_track_package_identifier(self):
6268
self.logger.debug(TrackPackageIdentifier)
6369
self.TrackPackageIdentifier = TrackPackageIdentifier
6470

71+
def _check_response_for_request_errors(self):
72+
"""
73+
Checks the response to see if there were any errors specific to
74+
this WSDL.
75+
"""
76+
if self.response.HighestSeverity == "ERROR":
77+
for notification in self.response.Notifications:
78+
if "Invalid tracking number" in notification.Message:
79+
raise FedexInvalidTrackingNumber(notification.Code,
80+
notification.Message)
81+
6582
def _assemble_and_send_request(self):
6683
"""
6784
Fires off the Fedex request.

0 commit comments

Comments
 (0)