Skip to content
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

Add 404 error handling #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion QualtricsAPI/Exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class Qualtrics403Error(Exception):
def __init__(self, msg):
super().__init__(msg)

class Qualtrics404Error(Exception):
'''This Exception handles errors associated with the HTTP 404 (Not Found) responses.'''
def __init__(self, msg):
super().__init__(msg)

class Qualtrics429Error(Exception):
'''This Exception handles errors associated with the HTTP 429 (Too Many Requests) responses.'''
def __init__(self, msg):
Expand All @@ -37,4 +42,4 @@ def __init__(self, msg):
class Qualtrics504Error(Exception):
'''This Exception handles errors associated with the HTTP 504 (Gateway Timeout) responses.'''
def __init__(self, msg):
super().__init__(msg)
super().__init__(msg)
33 changes: 26 additions & 7 deletions QualtricsAPI/Survey/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dateutil.parser import parse
from QualtricsAPI.Setup import Credentials
from QualtricsAPI.JSON import Parser
from QualtricsAPI.Exceptions import Qualtrics500Error, Qualtrics503Error, Qualtrics504Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error
from QualtricsAPI.Exceptions import Qualtrics500Error, Qualtrics503Error, Qualtrics504Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error, Qualtrics404Error
import warnings
import time

Expand Down Expand Up @@ -121,7 +121,10 @@ def setup_request_v3(self, survey=None, payload=None, verify=None):
elif response['meta']['httpStatus'] == '403 - Forbidden':
raise Qualtrics403Error(
'Qualtrics Error\n(Http Error: 403 - Forbidden): The Qualtrics API user was authenticated and made a valid request, but is not authorized to access this requested resource.')
except (Qualtrics500Error, Qualtrics503Error, Qualtrics504Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error) as e:
elif response['meta']['httpStatus'] == '404 - Not Found':
raise Qualtrics404Error(
'Qualtrics Error\n(Http Error: 404 - Not Found): The Qualtrics API user was authenticated and made a valid request, but the survey could not be found. Verify the survey ID and ensure you have the necessary permissions.')
except (Qualtrics500Error, Qualtrics503Error, Qualtrics504Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error, Qualtrics404Error) as e:
return print(e)
else:
progress_id = response['result']['progressId']
Expand Down Expand Up @@ -161,7 +164,10 @@ def send_request_v3(self, survey=None, payload=None, verify=None):
elif check_response['meta']['httpStatus'] == '403 - Forbidden':
raise Qualtrics403Error(
'Qualtrics Error\n(Http Error: 403 - Forbidden): The Qualtrics API user was authenticated and made a valid request, but is not authorized to access this requested resource.')
except (Qualtrics500Error, Qualtrics503Error, Qualtrics504Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error) as e:
elif check_response['meta']['httpStatus'] == '404 - Not Found':
raise Qualtrics404Error(
'Qualtrics Error\n(Http Error: 404 - Not Found): The Qualtrics API user was authenticated and made a valid request, but the survey could not be found. Verify the survey ID and ensure you have the necessary permissions.')
except (Qualtrics500Error, Qualtrics503Error, Qualtrics504Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error, Qualtrics404Error) as e:
return print(e)
else:
download_url = url + is_file + '/file'
Expand Down Expand Up @@ -364,10 +370,14 @@ def get_survey_response(self, survey=None, response=None, verbose=False, verify=
elif response['meta']['httpStatus'] == '403 - Forbidden':
raise Qualtrics403Error(
'Qualtrics Error\n(Http Error: 403 - Forbidden): The Qualtrics API user was authenticated and made a valid request, but is not authorized to access this requested resource.')
elif response['meta']['httpStatus'] == '404 - Not Found':
raise Qualtrics404Error(
'Qualtrics Error\n(Http Error: 404 - Not Found): The Qualtrics API user was authenticated and made a valid request, but the survey could not be found. Verify the survey ID and ensure you have the necessary permissions.')

except (Qualtrics503Error, Qualtrics504Error) as e:
# Recursive call to handle Internal Server Errors
return self.get_survey_response(self, survey=survey, response=response, verbose=verbose)
except (Qualtrics500Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error) as e:
except (Qualtrics500Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error, Qualtrics404Error) as e:
# Handle Authorization/Bad Request Errors
return print(e)
else:
Expand Down Expand Up @@ -407,10 +417,13 @@ def create_survey_response(self, survey=None, dynamic_payload={}, verbose=False,
elif response['meta']['httpStatus'] == '403 - Forbidden':
raise Qualtrics403Error(
'Qualtrics Error\n(Http Error: 403 - Forbidden): The Qualtrics API user was authenticated and made a valid request, but is not authorized to access this requested resource.')
elif response['meta']['httpStatus'] == '404 - Not Found':
raise Qualtrics404Error(
'Qualtrics Error\n(Http Error: 404 - Not Found): The Qualtrics API user was authenticated and made a valid request, but the survey could not be found. Verify the survey ID and ensure you have the necessary permissions.')
except (Qualtrics503Error, Qualtrics504Error) as e:
# Recursive call to handle Internal Server Errors
return self.create_survey_response(self, survey=survey, dynamic_payload=dynamic_payload, verify=verify)
except (Qualtrics500Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error) as e:
except (Qualtrics500Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error, Qualtrics404Error) as e:
# Handle Authorization/Bad Request Errors
return print(e, response['meta'])
else:
Expand Down Expand Up @@ -462,10 +475,13 @@ def update_survey_response_embedded_data(self, survey=None, response_id=None, em
elif response['meta']['httpStatus'] == '403 - Forbidden':
raise Qualtrics403Error(
'Qualtrics Error\n(Http Error: 403 - Forbidden): The Qualtrics API user was authenticated and made a valid request, but is not authorized to access this requested resource.')
elif response['meta']['httpStatus'] == '404 - Not Found':
raise Qualtrics404Error(
'Qualtrics Error\n(Http Error: 404 - Not Found): The Qualtrics API user was authenticated and made a valid request, but the survey could not be found. Verify the survey ID and ensure you have the necessary permissions.')
except (Qualtrics503Error, Qualtrics504Error) as e:
# Recursive call to handle Internal Server Errors
return self.update_survey_response_embedded_data(survey, response_id, embedded_data, reset_recorded_date)
except (Qualtrics500Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error) as e:
except (Qualtrics500Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error, Qualtrics404Error) as e:
# Handle Authorization/Bad Request Errors
return print(e, response['meta'])
else:
Expand Down Expand Up @@ -589,10 +605,13 @@ def __handle_qualtrics_exceptions(self, response):
elif response['meta']['httpStatus'] == '403 - Forbidden':
raise Qualtrics403Error(
'Qualtrics Error\n(Http Error: 403 - Forbidden): The Qualtrics API user was authenticated and made a valid request, but is not authorized to access this requested resource.')
elif response['meta']['httpStatus'] == '404 - Not Found':
raise Qualtrics404Error(
'Qualtrics Error\n(Http Error: 404 - Not Found): The Qualtrics API user was authenticated and made a valid request, but the survey could not be found. Verify the survey ID and ensure you have the necessary permissions.')
except (Qualtrics503Error, Qualtrics504Error) as e:
# Potential strategy for retry logic for retryable internal errors
return None
except (Qualtrics500Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error) as e:
except (Qualtrics500Error, Qualtrics400Error, Qualtrics401Error, Qualtrics403Error, Qualtrics404Error) as e:
return str(e), response['meta']
return None

Expand Down