Skip to content

Commit 5a96034

Browse files
author
Greg Taylor
committed
Add more error handling to the base service class. Throw SchemaValidationError exceptions instead of showing a big nasty (useless) suds traceback. Also catch general Fedex errors unless over-rridden on each class.
1 parent 38b2000 commit 5a96034

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

fedex/base_service.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99
import os
1010
import logging
11+
import suds
1112
from suds.client import Client
1213

1314
class FedexBaseServiceException(Exception):
@@ -34,6 +35,14 @@ class FedexError(FedexBaseServiceException):
3435
"""
3536
pass
3637

38+
class SchemaValidationError(FedexBaseServiceException):
39+
"""
40+
There is probably a problem in the data you provided.
41+
"""
42+
def __init__(self):
43+
self.error_code = -1
44+
self.value = "suds encountered an error validating your data against this service's WSDL schema. Please double-check for missing or invalid values, filling all required fields."
45+
3746
class FedexBaseService(object):
3847
"""
3948
This class is the master class for all Fedex request objects. It gets all
@@ -173,7 +182,11 @@ def _check_response_for_request_errors(self):
173182
specific to that module. For example, invalid tracking numbers in
174183
a Tracking request.
175184
"""
176-
pass
185+
if self.response.HighestSeverity == "ERROR":
186+
for notification in self.response.Notifications:
187+
if notification.Severity == "ERROR":
188+
raise FedexError(notification.Code,
189+
notification.Message)
177190

178191
def create_wsdl_object_of_type(self, type_name):
179192
"""
@@ -186,13 +199,21 @@ def send_request(self):
186199
Sends the assembled request on the child object.
187200
"""
188201
# Send the request and get the response back.
189-
self.response = self._assemble_and_send_request()
202+
try:
203+
self.response = self._assemble_and_send_request()
204+
except suds.WebFault:
205+
# When this happens, throw an informative message reminding the
206+
# user to check all required variables, making sure they are
207+
# populated and valid.
208+
raise SchemaValidationError()
209+
190210
# Check the response for general Fedex errors/failures that aren't
191211
# specific to any given WSDL/request.
192212
self.__check_response_for_fedex_error()
193213
# Check the response for errors specific to the particular request.
194214
# This is handled by an overridden method on the child object.
195215
self._check_response_for_request_errors()
216+
196217
# Debug output.
197218
self.logger.info("== FEDEX QUERY RESULT ==")
198219
self.logger.info(self.response)

0 commit comments

Comments
 (0)