diff --git a/cekit/tools.py b/cekit/tools.py
index e5f8e52..624434a 100644
--- a/cekit/tools.py
+++ b/cekit/tools.py
@@ -83,6 +83,20 @@ def get_brew_url(md5):
         get_build_cmd = ['brew', 'call', '--json-output', 'getBuild', 'buildInfo=%s' % build_id]
         logger.debug("Executing '%s'" % " ".join(get_build_cmd))
         build = yaml.safe_load(subprocess.check_output(get_build_cmd))
+
+        build_states = ['BUILDING', 'COMPLETE', 'DELETED', 'FAILED', 'CANCELED']
+
+        # State 1 means: COMPLETE which is the only success state. Other states are:
+        #
+        # 'BUILDING': 0
+        # 'COMPLETE': 1
+        # 'DELETED': 2
+        # 'FAILED': 3
+        # 'CANCELED': 4
+        if build['state'] != 1:
+            raise CekitError(
+                "Artifact with checksum {} was found in Koji metadata but the build is in incorrect state ({}) making the artifact not available for downloading anymore".format(md5, build_states[build['state']]))
+
         package = build['package_name']
         release = build['release']
 

diff --git a/tests/test_unit_tools.py b/tests/test_unit_tools.py
index 4ea9fdb..421b7e9 100644
--- a/tests/test_unit_tools.py
+++ b/tests/test_unit_tools.py
@@ -139,7 +139,7 @@ def test_merge_run_cmd():
     assert override['user'] == 'foo'
 
 
-def brew_call(*args, **kwargs):
+def brew_call_ok(*args, **kwargs):
     if 'listArchives' in args[0]:
         return """
         [
@@ -155,19 +155,53 @@ def brew_call(*args, **kwargs):
         return """
         {
           "package_name": "package_name",
-          "release": "release"
+          "release": "release",
+          "state": 1
+        }
+        """
+    return ""
+
+
+def brew_call_removed(*args, **kwargs):
+    if 'listArchives' in args[0]:
+        return """
+        [
+          {
+            "build_id": "build_id",
+            "filename": "filename",
+            "group_id": "group_id",
+            "artifact_id": "artifact_id",
+            "version": "version",
+          }
+        ]"""
+    if 'getBuild' in args[0]:
+        return """
+        {
+          "package_name": "package_name",
+          "release": "release",
+          "state": 2
         }
         """
     return ""
 
 
 def test_get_brew_url(mocker):
-    mocker.patch('subprocess.check_output', side_effect=brew_call)
+    mocker.patch('subprocess.check_output', side_effect=brew_call_ok)
     url = tools.get_brew_url('aa')
     assert url == "http://download.devel.redhat.com/brewroot/packages/package_name/" + \
         "version/release/maven/group_id/artifact_id/version/filename"
 
 
+def test_get_brew_url_when_build_was_removed(mocker):
+    mocker.patch('subprocess.check_output', side_effect=brew_call_removed)
+
+    with pytest.raises(CekitError) as excinfo:
+        tools.get_brew_url('aa')
+
+    assert 'Artifact with checksum aa was found in Koji metadata but the build is in incorrect state (DELETED) making the artifact not available for downloading anymore' in str(
+        excinfo.value)
+
+
 @contextmanager
 def mocked_dependency_handler(mocker, data="ID=fedora\nNAME=somefedora\nVERSION=123"):
     dh = None

Handle deleted artifacts in koji
When an artifact is deleted we should not try to fetch it. Instead the build should be terminated.
tests/test_unit_tools.py::test_get_brew_url_when_build_was_removed
tests/test_unit_tools.py::test_merging_description_image tests/test_unit_tools.py::test_merging_description_modules tests/test_unit_tools.py::test_merging_description_override tests/test_unit_tools.py::test_merging_plain_descriptors tests/test_unit_tools.py::test_merging_emdedded_descriptors tests/test_unit_tools.py::test_merging_plain_lists tests/test_unit_tools.py::test_merging_plain_list_of_list tests/test_unit_tools.py::test_merging_list_of_descriptors tests/test_unit_tools.py::test_merge_run_cmd tests/test_unit_tools.py::test_get_brew_url tests/test_unit_tools.py::test_dependency_handler_init_on_unknown_env_with_os_release_file tests/test_unit_tools.py::test_dependency_handler_init_on_known_env tests/test_unit_tools.py::test_dependency_handler_init_on_unknown_env_without_os_release_file tests/test_unit_tools.py::test_dependency_handler_handle_dependencies_doesnt_fail_without_deps tests/test_unit_tools.py::test_dependency_handler_handle_dependencies_with_executable_only tests/test_unit_tools.py::test_dependency_handler_handle_dependencies_with_executable_only_failed tests/test_unit_tools.py::test_dependency_handler_handle_dependencies_with_executable_and_package_on_known_platform tests/test_unit_tools.py::test_dependency_handler_handle_dependencies_with_platform_specific_package tests/test_unit_tools.py::test_dependency_handler_check_for_executable_with_executable_only tests/test_unit_tools.py::test_dependency_handler_check_for_executable_with_executable_fail tests/test_unit_tools.py::test_dependency_handler_check_for_executable_with_executable_fail_with_package