How to handle errors with boto3?

后端 未结 10 890
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 04:02

I am trying to figure how to do proper error handling with boto3.

I am trying to create an IAM user:

def create_user(username, iam_conn):
    try:
           


        
10条回答
  •  执念已碎
    2020-12-02 04:44

    I found it very useful, since the Exceptions are not documented, to list all exceptions to the screen for this package. Here is the code I used to do it:

    import botocore.exceptions
    def listexns(mod):
        #module = __import__(mod)
        exns = []
        for name in botocore.exceptions.__dict__:
            if (isinstance(botocore.exceptions.__dict__[name], Exception) or
                name.endswith('Error')):
                exns.append(name)
        for name in exns:
            print('%s.%s is an exception type' % (str(mod), name))
        return
    
    if __name__ == '__main__':
        import sys
        if len(sys.argv) <= 1:
            print('Give me a module name on the $PYTHONPATH!')
        print('Looking for exception types in module: %s' % sys.argv[1])
        listexns(sys.argv[1])
    

    Which results in:

    Looking for exception types in module: boto3
    boto3.BotoCoreError is an exception type
    boto3.DataNotFoundError is an exception type
    boto3.UnknownServiceError is an exception type
    boto3.ApiVersionNotFoundError is an exception type
    boto3.HTTPClientError is an exception type
    boto3.ConnectionError is an exception type
    boto3.EndpointConnectionError is an exception type
    boto3.SSLError is an exception type
    boto3.ConnectionClosedError is an exception type
    boto3.ReadTimeoutError is an exception type
    boto3.ConnectTimeoutError is an exception type
    boto3.ProxyConnectionError is an exception type
    boto3.NoCredentialsError is an exception type
    boto3.PartialCredentialsError is an exception type
    boto3.CredentialRetrievalError is an exception type
    boto3.UnknownSignatureVersionError is an exception type
    boto3.ServiceNotInRegionError is an exception type
    boto3.BaseEndpointResolverError is an exception type
    boto3.NoRegionError is an exception type
    boto3.UnknownEndpointError is an exception type
    boto3.ConfigParseError is an exception type
    boto3.MissingParametersError is an exception type
    boto3.ValidationError is an exception type
    boto3.ParamValidationError is an exception type
    boto3.UnknownKeyError is an exception type
    boto3.RangeError is an exception type
    boto3.UnknownParameterError is an exception type
    boto3.AliasConflictParameterError is an exception type
    boto3.PaginationError is an exception type
    boto3.OperationNotPageableError is an exception type
    boto3.ChecksumError is an exception type
    boto3.UnseekableStreamError is an exception type
    boto3.WaiterError is an exception type
    boto3.IncompleteReadError is an exception type
    boto3.InvalidExpressionError is an exception type
    boto3.UnknownCredentialError is an exception type
    boto3.WaiterConfigError is an exception type
    boto3.UnknownClientMethodError is an exception type
    boto3.UnsupportedSignatureVersionError is an exception type
    boto3.ClientError is an exception type
    boto3.EventStreamError is an exception type
    boto3.InvalidDNSNameError is an exception type
    boto3.InvalidS3AddressingStyleError is an exception type
    boto3.InvalidRetryConfigurationError is an exception type
    boto3.InvalidMaxRetryAttemptsError is an exception type
    boto3.StubResponseError is an exception type
    boto3.StubAssertionError is an exception type
    boto3.UnStubbedResponseError is an exception type
    boto3.InvalidConfigError is an exception type
    boto3.InfiniteLoopConfigError is an exception type
    boto3.RefreshWithMFAUnsupportedError is an exception type
    boto3.MD5UnavailableError is an exception type
    boto3.MetadataRetrievalError is an exception type
    boto3.UndefinedModelAttributeError is an exception type
    boto3.MissingServiceIdError is an exception type
    

提交回复
热议问题