简体   繁体   中英

get http AWS server error code in python with boto3

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?

The botocore exception "ClientError" catches all exceptions from the AWS services. You have to search through the API of the corresponding AWS services. For example, the S3 API has a bunch of error number including the range 5xx. Your current implementation should already output error code 500 in case you have an "internal server error". (Of course getting an E 500 from AWS S3 is highly unlikely)

There are also a bunch of other botocore exceptions which might be useful to you, but note that they are static, so they won't give you as much information as the errors from the AWS services received through Client Error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM