Skip to content

Commit

Permalink
WIP Use exceptions instead of HTML responses with error statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
SpecLad committed Oct 1, 2024
1 parent 7551d6c commit a1f6618
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 206 deletions.
41 changes: 17 additions & 24 deletions cvat/apps/engine/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,9 @@ def handle_local_download() -> Response:
"Please request export first by sending a request without the action=download parameter."
)
if not rq_job:
return (
None
if action != "download"
else HttpResponseBadRequest(msg_no_such_job_when_downloading)
)
if action == "download":
raise serializers.ValidationError(msg_no_such_job_when_downloading)
return None

# define status once to avoid refreshing it on each check
# FUTURE-TODO: get_status will raise InvalidJobOperation exception instead of returning None in one of the next releases
Expand All @@ -273,15 +271,14 @@ def handle_local_download() -> Response:
# handle cases where the status is None for some reason
if not rq_job_status:
rq_job.delete()
return (
None
if action != "download"
else HttpResponseBadRequest(msg_no_such_job_when_downloading)
)

if action == "download":
raise serializers.ValidationError(msg_no_such_job_when_downloading)
return None

if action == "download":
if self.export_args.location != Location.LOCAL:
return HttpResponseBadRequest(
raise serializers.ValidationError(
'Action "download" is only supported for a local dataset location'
)
if rq_job_status not in {
Expand All @@ -290,7 +287,7 @@ def handle_local_download() -> Response:
RQJobStatus.CANCELED,
RQJobStatus.STOPPED,
}:
return HttpResponseBadRequest("Dataset export has not been finished yet")
raise serializers.ValidationError("Dataset export has not been finished yet")

instance_update_time = self.get_instance_update_time()
instance_timestamp = self.get_timestamp(instance_update_time)
Expand Down Expand Up @@ -543,11 +540,9 @@ def _handle_rq_job_v1(
"Please request export first by sending a request without the action=download parameter."
)
if not rq_job:
return (
None
if action != "download"
else HttpResponseBadRequest(msg_no_such_job_when_downloading)
)
if action == "download":
raise serializers.ValidationError(msg_no_such_job_when_downloading)
return None

# define status once to avoid refreshing it on each check
# FUTURE-TODO: get_status will raise InvalidJobOperation exception instead of None in one of the next releases
Expand All @@ -556,15 +551,13 @@ def _handle_rq_job_v1(
# handle cases where the status is None for some reason
if not rq_job_status:
rq_job.delete()
return (
None
if action != "download"
else HttpResponseBadRequest(msg_no_such_job_when_downloading)
)
if action == "download":
raise serializers.ValidationError(msg_no_such_job_when_downloading)
return None

if action == "download":
if self.export_args.location != Location.LOCAL:
return HttpResponseBadRequest(
raise serializers.ValidationError(
'Action "download" is only supported for a local backup location'
)
if rq_job_status not in {
Expand All @@ -573,7 +566,7 @@ def _handle_rq_job_v1(
RQJobStatus.CANCELED,
RQJobStatus.STOPPED,
}:
return HttpResponseBadRequest("Backup export has not been finished yet")
raise serializers.ValidationError("Backup export has not been finished yet")

if rq_job_status == RQJobStatus.FINISHED:
if self.export_args.location == Location.CLOUD_STORAGE:
Expand Down
Loading

0 comments on commit a1f6618

Please sign in to comment.