0

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?

2
  • What do you consider to be a "server error"? Can you provide an example that you've experienced? Commented Apr 21, 2020 at 10:14
  • That the problem, I'm not sure what exactly I'm looking for but a more general example is internal server error Http code 500 or service is unavailable Http code 503. I'm looking for something to catch this error and their message Http code just like the ClientError catches all errors that have Http 4XX code Commented Apr 21, 2020 at 10:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.