"""
All Mypolr exceptions are inherited from :class:`MypolrError` and are defined in this file.
"""
from functools import wraps
[docs]def no_raise(f):
"""Decorator/wrapper function to force return None instead of raising module exceptions.
Exceptions that can be ignored are found in mypolr.exceptions."""
@wraps(f)
def new_f(*args, **kwargs):
try:
return f(*args, **kwargs)
except MypolrError:
pass
return None
return new_f
[docs]class MypolrError(Exception):
"""
Base class for all module exceptions
"""
[docs]class DebugTempWarning(MypolrError):
"""Temporary Warning that should be removed"""
def __init__(self):
super(DebugTempWarning, self).__init__('This should not happen')
[docs]class CustomEndingUnavailable(MypolrError):
"""Raised when a custom ending is in use and therefore cannot be created.
:param str custom_ending: the custom ending that was unavailable
"""
def __init__(self, custom_ending):
msg = 'Custom ending already in use: {}'.format(custom_ending)
super(CustomEndingUnavailable, self).__init__(msg)
[docs]class BadApiRequest(MypolrError):
"""Raised when a request is malformed or otherwise is not understandable by server."""
def __init__(self):
msg = 'HTTP 400 Bad Request: Request malformed, or arguments do not fit the required data type.'
super(BadApiRequest, self).__init__(msg)
[docs]class BadApiResponse(MypolrError):
"""Raised when a response is malformed and cannot be interpreted as valid JSON."""
def __init__(self, msg='Cannot interpret API response: invalid JSON.'):
super(BadApiResponse, self).__init__(msg)
[docs]class UnauthorizedKeyError(MypolrError):
"""Raised when an invalid key has been used in a request.
This refers either to:
the API_KEY used in all endpoints, or the URL_KEY optionally used at the lookup endpoint.
:param msg:
:type msg:
"""
def __init__(self, msg=None):
msg = msg or 'API_KEY invalid or inactive.'
super(UnauthorizedKeyError, self).__init__('HTTP 401 Unauthorized: {}'.format(msg))
[docs]class QuotaExceededError(MypolrError):
"""Admins may assign quotas to users, and this is raised when it's exceeded and service stopped."""
def __init__(self):
msg = 'HTTP 403 Forbidden: quota is exceeded.'
super(QuotaExceededError, self).__init__(msg)
[docs]class ServerOrConnectionError(MypolrError):
"""Raised when there is a timeout, internal server error, or any other connection error."""
def __init__(self, caused=None):
msg = 'API cannot be reached. Check connection or server status.'
if caused is not None:
msg = '{} ({})'.format(msg, caused)
super(ServerOrConnectionError, self).__init__(msg)