From 09ddbf880756c0029b164e06ea4ae395afaf5653 Mon Sep 17 00:00:00 2001
From: Sorin Sbarnea <ssbarnea@redhat.com>
Date: Tue, 14 Apr 2020 18:13:07 +0100
Subject: [PATCH] Improve 404 error message on download-logs.sh

Includes reasons why a user may get 404 errors from zuul in order
to avoid confusions.

Change-Id: Iba39e85a9f84ec8f999b0c0ddce76597b4349257
---
 .../templates/download-logs.sh.j2             | 28 +++++++++++++------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/roles/local-log-download/templates/download-logs.sh.j2 b/roles/local-log-download/templates/download-logs.sh.j2
index 7047e1290..a1f7fc94b 100644
--- a/roles/local-log-download/templates/download-logs.sh.j2
+++ b/roles/local-log-download/templates/download-logs.sh.j2
@@ -26,15 +26,27 @@ function get_urls {
 import gzip
 import json
 import urllib.request
+from urllib.error import HTTPError
+import sys
 
-base_url = urllib.request.urlopen("${ZUUL_API_URL}").read()
-base_json = json.loads(base_url)
-manifest_url = [x['url'] for x in base_json['artifacts'] if x.get('metadata', {}).get('type') == 'zuul_manifest'][0]
-manifest = urllib.request.urlopen(manifest_url)
-if manifest.info().get('Content-Encoding') == 'gzip':
-    manifest_json = json.loads(gzip.decompress(manifest.read()))
-else:
-    manifest_json = json.loads(manifest.read())
+try:
+    base_url = urllib.request.urlopen("${ZUUL_API_URL}").read()
+    base_json = json.loads(base_url)
+    manifest_url = [x['url'] for x in base_json['artifacts'] if x.get('metadata', {}).get('type') == 'zuul_manifest'][0]
+    manifest = urllib.request.urlopen(manifest_url)
+    if manifest.info().get('Content-Encoding') == 'gzip':
+        manifest_json = json.loads(gzip.decompress(manifest.read()))
+    else:
+        manifest_json = json.loads(manifest.read())
+except HTTPError as e:
+    if e.code == 404:
+        print(
+            "Could not find build UUID in Zuul API. This can happen with "
+            "buildsets still running, or aborted ones. Try again after the "
+            "buildset is reported back to Zuul.", file=sys.stderr)
+    else:
+        print(e, file=sys.stderr)
+    sys.exit(2)
 
 def p(node, parent):
     if node.get('mimetype') != 'application/directory':