Skip to content

Commit

Permalink
Improve diagnostics
Browse files Browse the repository at this point in the history
The most interesting part of this is adding a repr() method to the base
AwsManaged class that prints the name of the object where available.

The rest of the change replaces some calls to logger.error() with
exceptions and adds information to exception strings.
  • Loading branch information
Klee Dienes authored and hartmans committed Jul 20, 2023
1 parent ee52dab commit 090253c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
29 changes: 23 additions & 6 deletions carthage_aws/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,27 +223,44 @@ async def possible_ids_for_name(self):
return [obj['ResourceId'] for obj in objs]
return []

def __repr__(self):
if self.name:
return f'<{self.__class__.__name__} ({self.name}) at 0x{id(self):0x}>'
else:
return f'<anonymous {self.__class__.__name__} at 0x{id(self):0x}>'

@setup_task("construct", order=700)
async def find_or_create(self):
if self.mob: return

if self.mob:
return

# If we are called directly, rather than through setup_tasks,
# then our check_completed will not have run, so we should
# explicitly try find, because double creating is bad.

await self.find()

if self.mob:
await self.ainjector(self.post_find_hook)
return
if not self.name: raise RuntimeError('You must specify a name for creation')

if not self.name:
raise RuntimeError(f'unable to create AWS resource for {self} without a name')
if self.readonly:
raise LookupError(f'{self.__class__.__name__} with name {self.name} not found and readonly enabled.')
raise LookupError(f'unable to find AWS resource for {self} and creation was not enabled')

await self.ainjector(self.pre_create_hook)
await run_in_executor(self.do_create)
assert self.mob or self.id

if not (self.mob or self.id):
raise RuntimeError(f'do_create failed to create AWS resource for {self}')

if not self.mob:
await self.find()
else:
if not self.id: self.id = self.mob.id
elif not self.id:
self.id = self.mob.id

await self.ainjector(self.post_create_hook)
await self.ainjector(self.post_find_hook)
return self.mob
Expand Down
2 changes: 1 addition & 1 deletion carthage_aws/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def do_create(self):
# No need to associate subnet with main route table

except ClientError as e:
logger.error(f'Could not create AWS subnet {self.name} due to {e}.')
raise RuntimeError(f'unable to create AWS subnet for {self}: {e}')

@inject_autokwargs(
ip_address = InjectionKey('ip_address', _optional=NotPresent))
Expand Down

0 comments on commit 090253c

Please sign in to comment.