Skip to content

Commit

Permalink
friendlier error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann Bahl committed Feb 12, 2024
1 parent 6ab69ac commit 4c92c8d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/backy/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ async def _pull_metadata(self, api: APIClient):
try:
await api.touch_backup(self.name)
remote_revs = await api.get_revs(self)

log.debug("pull-found-revs", revs=len(remote_revs))
except ClientResponseError as e:
if e.status in [
HTTPNotFound.status_code,
Expand All @@ -893,22 +893,20 @@ async def _pull_metadata(self, api: APIClient):
except ClientError:
log.warning("pull-error", exc_info=True)
remote_revs = []
log.debug(
"pull-found-revs",
revs=len(remote_revs),
)

matching_uuids = {
local_uuids = {
r.uuid for r in self.history if r.server == api.server_name
}
remote_uuids = {r.uuid for r in remote_revs}
for uuid in matching_uuids - remote_uuids:
for uuid in local_uuids - remote_uuids:
log.warning("pull-removing-unknown-rev", rev_uuid=uuid)
self.find_by_uuid(uuid).remove(force=True)

for r in remote_revs:
if r.uuid in local_uuids:
if r.to_dict() == self.find_by_uuid(r.uuid).to_dict():
continue
log.debug("pull-updating-rev", rev_uid=r.uuid)
else:
log.debug("pull-new-rev", rev_uid=r.uuid)
r.write_info()
log.debug(
"pull-updated-rev",
rev_uid=r.uuid,
)
27 changes: 27 additions & 0 deletions src/backy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,43 @@ def verify(self, revision):

def client(self, config, peer, url, token, apifunc, **kwargs):
async def run():
if peer and (url or token):
self.log.error(
"client-argparse-error",
_fmt_msg="--peer conflicts with --url and --token",
)
sys.exit(1)
if bool(url) ^ bool(token):
self.log.error(
"client-argparse-error",
_fmt_msg="--url and --token require each other",
)
sys.exit(1)
if url and token:
api = APIClient("<server>", url, token, self.taskid, self.log)
else:
d = backy.daemon.BackyDaemon(config, self.log)
d._read_config()
if peer:
if peer not in d.peers:
self.log.error(
"client-peer-unknown",
_fmt_msg="The peer {peer} is not known. Select a known peer or specify --url and --token.\n"
"The following peers are known: {known}",
peer=peer,
known=", ".join(d.peers.keys()),
)
sys.exit(1)
api = APIClient.from_conf(
peer, d.peers[peer], self.taskid, self.log
)
else:
if "token" not in d.api_cli_default:
self.log.error(
"client-missing-defaults",
_fmt_msg="The config file is missing default parameters. Please specify --url and --token",
)
sys.exit(1)
api = APIClient.from_conf(
"<server>", d.api_cli_default, self.taskid, self.log
)
Expand Down
18 changes: 18 additions & 0 deletions src/backy/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ async def test_simple_sync(daemons, log):
assert new_rev1.trust == rev1.trust
assert new_rev1.server == "server-1"

rev1.distrust()
rev1.tags = {"manual:new"}
rev1.write_info()

await j0.pull_metadata()
b0.scan()

assert [r.uuid for r in b0.history] == [rev0.uuid, rev1.uuid]
new_rev1 = b0.history[1]
assert new_rev1.backup == b0
assert new_rev1.timestamp == rev1.timestamp
assert new_rev1.backend_type == ""
assert new_rev1.stats == rev1.stats
assert new_rev1.tags == rev1.tags
assert new_rev1.orig_tags == rev1.tags
assert new_rev1.trust == rev1.trust
assert new_rev1.server == "server-1"

new_rev1.remove()
assert [r.uuid for r in b0.history] == [rev0.uuid, rev1.uuid]
assert new_rev1.tags == set()
Expand Down

0 comments on commit 4c92c8d

Please sign in to comment.