def error_checked_function(self, *args, **kwargs): return_value = function(self, *args, **kwargs) allowed = ('captcha', 'data', 'errors', 'kind', 'names', 'next', 'prev', 'ratelimit', 'users') if isinstance(return_value, dict): for key in return_value: if key not in allowed: warnings.warn_explicit('Unknown return key: %s' % key, UserWarning, '', 0) if 'errors' in return_value and return_value['errors']: error_list = [] for error_type, msg, value in return_value['errors']: if error_type in errors.ERROR_MAPPING: if error_type == 'RATELIMIT': _request.evict(args[0]) error_class = errors.ERROR_MAPPING[error_type] else: error_class = errors.APIException error_list.append( error_class(error_type, msg, value, return_value)) if len(error_list) == 1: raise error_list[0] else: raise errors.ExceptionList(error_list) return return_value
def wrapped(reddit_session, *args, **kwargs): try: return_value = function(reddit_session, *args, **kwargs) except HTTPError as exc: if exc.response.status_code != 400: raise # Unhandled HTTPErrors try: # Attempt to convert v1 errors into older format (for now) data = exc.response.json() assert len(data) == 2 return_value = { 'errors': [(data['reason'], data['explanation'], '')] } except Exception: raise exc if isinstance(return_value, dict): if return_value.get('error') == 304: # Not modified exception raise errors.NotModified(return_value) elif return_value.get('errors'): error_list = [] for error_type, msg, value in return_value['errors']: if error_type in errors.ERROR_MAPPING: if error_type == 'RATELIMIT': reddit_session.evict(args[0]) error_class = errors.ERROR_MAPPING[error_type] else: error_class = errors.APIException error_list.append( error_class(error_type, msg, value, return_value)) if len(error_list) == 1: raise error_list[0] else: raise errors.ExceptionList(error_list) return return_value
def wrapped(reddit_session, *args, **kwargs): return_value = function(reddit_session, *args, **kwargs) if isinstance(return_value, dict): if return_value.get('error') == 304: # Not modified exception raise errors.NotModified(return_value) elif return_value.get('errors'): error_list = [] for error_type, msg, value in return_value['errors']: if error_type in errors.ERROR_MAPPING: if error_type == 'RATELIMIT': reddit_session.evict(args[0]) error_class = errors.ERROR_MAPPING[error_type] else: error_class = errors.APIException error_list.append( error_class(error_type, msg, value, return_value)) if len(error_list) == 1: raise error_list[0] else: raise errors.ExceptionList(error_list) return return_value
def raise_api_exceptions(function, *args, **kwargs): """Raise client side exception(s) when present in the API response. Returned data is not modified. """ try: return_value = function(*args, **kwargs) except errors.HTTPException as exc: if exc._raw.status_code != 400: # pylint: disable=W0212 raise # Unhandled HTTPErrors try: # Attempt to convert v1 errors into older format (for now) data = exc._raw.json() # pylint: disable=W0212 assert len(data) == 2 return_value = { 'errors': [(data['reason'], data['explanation'], '')] } except Exception: raise exc if isinstance(return_value, dict): if return_value.get('error') == 304: # Not modified exception raise errors.NotModified(return_value) elif return_value.get('errors'): error_list = [] for error_type, msg, value in return_value['errors']: if error_type in errors.ERROR_MAPPING: if error_type == 'RATELIMIT': args[0].evict(args[1]) error_class = errors.ERROR_MAPPING[error_type] else: error_class = errors.APIException error_list.append( error_class(error_type, msg, value, return_value)) if len(error_list) == 1: raise error_list[0] else: raise errors.ExceptionList(error_list) return return_value