I have a decorator which should catch errors from the request:
def catch_request_errors(func):
@functools.wraps(func)
def inner_function(*args, **kwargs): # -> Optional[Dict]
try:
response = func(*args, **kwargs)
except Exception as e:
capture_message(str(e), level='error')
errors = {'error': str(e)}
return errors
if not response.ok:
capture_message(response.text, level='error')
errors = {'error': str(response.text)}
return errors
return None
return inner_function
The decorator itself returns Optional[Dict] - when error occurs should return {'error': 'error_msg_here'} or None if there are no errors.
Here is my decorated function, which actually returns Response without decorator, but with decorator it returns Optional[Dict]:
@catch_request_errors
def make_request(self, url: str, data: Dict): # -> Optional[Dict]
return requests.post(url, data) # but there is actually -> Response
I had to "hard type" like that to bamboozle my IDE, but how should I do it properly?
request_errors = self.make_request(settings.DOCUMENT_GENERATOR_URL, data) # type: Optional[Dict]
Somewhat similar solution like in subprocess.Popen.comunicate