I'm building a system that works a lot with AWS using boto3 in python and I need to log every error that occurs during the running process.
I know how to catch client errors (if exists represented using 4XX Http code), But I didn't manage to find a way to catch server error and get their Http code(5XX)
For now, I have this code:
start = time.time()
try:
# This can be any boto3 function, create volume is just an example
boto3.client('ec2').create_volume(...)
except ClientError as e:
# Collect all required data
end = time.time()
http_code = e.response.get('ResponseMetadata', {}).get('HTTPStatusCode')
retry_attempts = e.response.get('ResponseMetadata', {}).get('RetryAttempts')
# Log error with the collected data
log_error(tag, f'{type(e)}: {e}', http_code=http_code, response_time=end - start,
retry_attempts=retry_attempts)
except BotoCoreError as e:
# Collect all required data
end = time.time()
# Log error with the collected data
log_error(tag, f'{type(e)}: {e}', response_time=end - start)
Is there a way to catch server error and get their Http code similar to what I have done with the client Error?