# Buggy code
def load_ecr_repositories(
    neo4j_session, data, region, current_aws_account_id, aws_update_tag
):
    query = """
    MERGE (repo:ECRRepository{id: {RepositoryArn}})
    ON CREATE SET repo.firstseen = timestamp(), repo.arn = {RepositoryArn}, repo.name = {RepositoryName},
        repo.region = {Region}, repo.created_at = {CreatedAt}
    SET repo.lastupdated = {aws_update_tag}, repo.uri = {RepositoryUri}
    WITH repo
    MATCH (owner:AWSAccount{id: {AWS_ACCOUNT_ID}})
    MERGE (owner)-[r:RESOURCE]->(repo)
    ON CREATE SET r.firstseen = timestamp()
    SET r.lastupdated = {aws_update_tag}
    """

    for repo in data:
        neo4j_session.run(
            query,
            RepositoryArn=repo["repositoryArn"],
            RepositoryName=repo["repositoryName"],
            RepositoryUri=repo["repositoryUri"],
            CreatedAt=str(repo["createdAt"]),
            Region=region,
            aws_update_tag=aws_update_tag,
            AWS_ACCOUNT_ID=current_aws_account_id,
        )


# Traceback (ConnectionResetError)
Traceback (most recent call last):
File "{PATH}/lib/python3.6/site-packages/cartography/sync.py", line 71, in run
stage_func(neo4j_session, config)
File "{OTHER_PATH}/intelmodules/lyft/aws.py", line 234, in lyft_start_aws_ingestion
common_job_parameters
File "{PATH}/lib/python3.6/site-packages/cartography/intel/aws/__init__.py", line 98, in _sync_multiple_accounts
_sync_one_account(neo4j_session, boto3_session, account_id, sync_tag, common_job_parameters)
File "{PATH}/lib/python3.6/site-packages/cartography/intel/aws/__init__.py", line 46, in _sync_one_account
ecr.sync(neo4j_session, boto3_session, regions, account_id, sync_tag, common_job_parameters)
File "{PATH}/lib/python3.6/site-packages/cartography/util.py", line 58, in timed
result = method(*args, **kwargs)
File "{PATH}/lib/python3.6/site-packages/cartography/intel/aws/ecr.py", line 241, in sync
load_ecr_repository_images(neo4j_session, image_data, region, aws_update_tag)
File "{PATH}/lib/python3.6/site-packages/cartography/util.py", line 58, in timed
result = method(*args, **kwargs)
File "{PATH}/lib/python3.6/site-packages/cartography/intel/aws/ecr.py", line 171, in load_ecr_repository_images
Region=region,
File "{PATH}/lib/python3.6/site-packages/neo4j/__init__.py", line 502, in run
self._connection.send()
File "{PATH}/lib/python3.6/site-packages/neobolt/direct.py", line 394, in send
self._send()
File "{PATH}/lib/python3.6/site-packages/neobolt/direct.py", line 409, in _send
self.socket.sendall(data)
File "/usr/lib/python3.6/ssl.py", line 965, in sendall
v = self.send(data[count:])
File "/usr/lib/python3.6/ssl.py", line 935, in send
return self._sslobj.write(data)
File "/usr/lib/python3.6/ssl.py", line 636, in write
return self._sslobj.write(data)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "{PATH}/lib/python3.6/site-packages/neo4j/__init__.py", line 399, in close
self._connection.sync()
File "{PATH}/lib/python3.6/site-packages/neobolt/direct.py", line 505, in sync
self.send()
File "{PATH}/lib/python3.6/site-packages/neobolt/direct.py", line 394, in send
self._send()
File "{PATH}/lib/python3.6/site-packages/neobolt/direct.py", line 409, in _send
self.socket.sendall(data)
File "/usr/lib/python3.6/ssl.py", line 965, in sendall
v = self.send(data[count:])
File "/usr/lib/python3.6/ssl.py", line 935, in send
return self._sslobj.write(data)
File "/usr/lib/python3.6/ssl.py", line 636, in write
return self._sslobj.write(data)
BrokenPipeError: [Errno 32] Broken pipe

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "{OTHER_PATH}/intelmodules/syncgraph.py", line 220, in <module>
main(argv)
File "{OTHER_PATH}/intelmodules/syncgraph.py", line 210, in main
return cartography.sync.run_with_config(sync, config)
File "{PATH}/lib/python3.6/site-packages/cartography/sync.py", line 145, in run_with_config
return sync.run(neo4j_driver, config)
File "{PATH}/lib/python3.6/site-packages/cartography/sync.py", line 78, in run
logger.info("Finishing sync stage '%s'", stage_name)
File "{PATH}/lib/python3.6/site-packages/neo4j/__init__.py", line 374, in __exit__
self.close()
File "{PATH}/lib/python3.6/site-packages/neo4j/__init__.py", line 401, in close
ServiceUnavailable, SessionError):
NameError: name 'ServiceUnavailable' is not defined

# Fixed code
def load_ecr_repositories(
    neo4j_session, data, region, current_aws_account_id, aws_update_tag
):
    query = """
    MERGE (repo:ECRRepository{id: {RepositoryArn}})
    ON CREATE SET repo.firstseen = timestamp(), repo.arn = {RepositoryArn}, repo.name = {RepositoryName},
        repo.region = {Region}, repo.created_at = {CreatedAt}
    SET repo.lastupdated = {aws_update_tag}, repo.uri = {RepositoryUri}
    WITH repo
    MATCH (owner:AWSAccount{id: {AWS_ACCOUNT_ID}})
    MERGE (owner)-[r:RESOURCE]->(repo)
    ON CREATE SET r.firstseen = timestamp()
    SET r.lastupdated = {aws_update_tag}
    """

    for repo in data:
        neo4j_session.run(
            query,
            RepositoryArn=repo["repositoryArn"],
            RepositoryName=repo["repositoryName"],
            RepositoryUri=repo["repositoryUri"],
            CreatedAt=str(repo["createdAt"]),
            Region=region,
            aws_update_tag=aws_update_tag,
            AWS_ACCOUNT_ID=current_aws_account_id,
        ).consume()  # See issue #440