Skip to content

Commit

Permalink
Raise an 'ArtifactNotFound' exception when requesting a non-existent …
Browse files Browse the repository at this point in the history
…artifact
  • Loading branch information
ahal committed Mar 24, 2020
1 parent 6f5300f commit 4666f78
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
18 changes: 17 additions & 1 deletion mozci/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class BasePushException(Exception):
def __init__(self, rev, branch, msg):
self.rev = rev
self.branch = branch
self.msg = f"Error processing push '{rev}' on {branch}: {msg}"
self.msg = f"Error with push '{rev}' on {branch}: {msg}"


class PushNotFound(BasePushException):
Expand All @@ -14,3 +14,19 @@ class PushNotFound(BasePushException):
def __init__(self, *args, **kwargs):
kwargs["msg"] = "does not exist!"
super(PushNotFound, self).__init__(*args, **kwargs)


class BaseTaskException(Exception):
def __init__(self, id, label, msg):
self.id = id
self.label = label
self.msg = f"Error with task '{id}' ({label}): {msg}"


class ArtifactNotFound(BaseTaskException):
"""Raised when the requested task artifact does not exist."""

def __init__(self, artifact, *args, **kwargs):
kwargs["msg"] = f"artifact '{artifact}' does not exist!"
self.artifact = artifact
super(ArtifactNotFound, self).__init__(*args, **kwargs)
14 changes: 13 additions & 1 deletion mozci/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from enum import Enum
from typing import Dict, List

import requests
from adr.util import memoize, memoized_property
from loguru import logger
from urllib3.response import HTTPResponse

from mozci.errors import ArtifactNotFound
from mozci.util.taskcluster import get_artifact, list_artifacts


Expand Down Expand Up @@ -94,8 +96,18 @@ def get_artifact(self, path):
Returns:
Contents of the artifact.
Raises:
mozci.errors.ArtifactNotFound: When the requested artifact does not
exist.
"""
data = get_artifact(self.id, path)
try:
data = get_artifact(self.id, path)
except requests.exceptions.HTTPError as e:
if e.response.status_code == 404:
raise ArtifactNotFound(path, self.id, self.label) from e
raise

if not isinstance(data, HTTPResponse):
return data

Expand Down
6 changes: 5 additions & 1 deletion mozci/util/taskcluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ def get_artifact(task_id, path):
"""
try:
response = _do_request(get_artifact_url(task_id, path))
except requests.exceptions.HTTPError:
except requests.exceptions.HTTPError as e:
if e.response.status_code != 404:
raise

response = _do_request(get_artifact_url(task_id, path, old_deployment=True))

return _handle_artifact(path, response)


Expand Down
40 changes: 40 additions & 0 deletions tests/test_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-

import pytest

from mozci.errors import ArtifactNotFound
from mozci.task import Task
from mozci.util.taskcluster import get_artifact_url


@pytest.fixture
def create_task():
id = 0

def inner(**kwargs):
nonlocal id
task = Task.create(id=id, **kwargs)
id += 1
return task

return inner


def test_missing_artifacts(responses, create_task):
artifact = "public/artifact.txt"
task = create_task(label="foobar")

# First we'll check the new deployment.
responses.add(
responses.GET, get_artifact_url(task.id, artifact), status=404,
)

# Then we'll check the old deployment.
responses.add(
responses.GET,
get_artifact_url(task.id, artifact, old_deployment=True),
status=404,
)

with pytest.raises(ArtifactNotFound):
task.get_artifact(artifact)

0 comments on commit 4666f78

Please sign in to comment.