def parse_s3_error_response()

in source/s3_helper.py [0:0]


def parse_s3_error_response(error_response):
    """
    Extract error code and message from original http response.
    For more details see https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html#ErrorCodeList
    """

    ERROR_RE = r'(?<=<Error>).*(?=<\/Error>)'
    CODE_RE = r'(?<=<Code>).*(?=<\/Code>)'
    MESSAGE_RE = r'(?<=<Message>).*(?=<\/Message>)'
    
    s3_error_code =  "InternalError "
    s3_error_message = "Unknown Error"

    # Remove line breaks and XML header
    xml = ''.join(error_response.split('\n')[1:])

    error_matched = re.search(ERROR_RE, xml)
    code_matched = re.search(CODE_RE, xml)
    message_matched = re.search(MESSAGE_RE, xml)
    
    if code_matched:
        s3_error_code = code_matched[0]
    if message_matched:
        s3_error_message = message_matched[0]

    return s3_error_code, s3_error_message