Refactor get_status and create_archive_task error handling

Raise exceptions instead of returning error messages from the worker in
create_arvive_task. This ensures consistency in how the errors are
presented on the task result: the Exception will be the result instead
of *maybe* being wrapped in an object like {error: Exception}.

This lets us simplify error handling in get_status so we have only one
try/except block where the error can be returned to the client.
This commit is contained in:
Lilia Kai
2023-09-06 13:54:32 +02:00
parent 00201770ba
commit f20dd05928
2 changed files with 19 additions and 16 deletions

View File

@@ -30,7 +30,9 @@ def get_db():
def create_archive_task(self, archive_json: str):
archive = schemas.ArchiveCreate.parse_raw(archive_json)
if (em := is_group_invalid_for_user(archive.public, archive.group_id, archive.author_id)): return {"error": em}
invalid = is_group_invalid_for_user(archive.public, archive.group_id, archive.author_id)
if (invalid):
raise Exception(invalid) # marks task FAILED, saves the Exception as reult
url = archive.url
logger.info(f"{url=} {archive=}")
@@ -40,9 +42,10 @@ def create_archive_task(self, archive_json: str):
try:
insert_result_into_db(result, archive.tags, archive.public, archive.group_id, archive.author_id, self.request.id)
except Exception as e:
# Log it, then raise again to store the error as the task result
logger.error(e)
logger.error(traceback.format_exc())
return {"error": e}
raise e
return result.to_dict()