From f9eeee0cc95e3306e1388de165af8cdf84c02ea0 Mon Sep 17 00:00:00 2001
From: Tero Kivinen <kivinen@iki.fi>
Date: Sat, 3 Nov 2018 11:08:18 +0000
Subject: [PATCH] Added easier to detect error message when search returns 0
 results. This currently parses html result from the mailarchive system, but
 when we get proper API to mailarchive system it should be changed to do that
 instead. Fixes #2126. Commit ready for merge.  - Legacy-Id: 15706

---
 ietf/doc/views_review.py | 5 ++++-
 ietf/review/mailarch.py  | 4 ++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/ietf/doc/views_review.py b/ietf/doc/views_review.py
index fe8bc3dcd..c1ba744a7 100644
--- a/ietf/doc/views_review.py
+++ b/ietf/doc/views_review.py
@@ -687,7 +687,10 @@ def search_mail_archive(request, name, request_id):
     try:
         res["messages"] = mailarch.retrieve_messages(res["query_data_url"])[:MAX_RESULTS]
     except Exception as e:
-        res["error"] = "Retrieval from mail archive failed: {}".format(unicode(e))
+        if unicode(e) == "NONE":
+            res["error"] = "No results found"
+        else:
+            res["error"] = "Retrieval from mail archive failed: {}".format(unicode(e))
         # raise # useful when debugging
 
     return JsonResponse(res)
diff --git a/ietf/review/mailarch.py b/ietf/review/mailarch.py
index 70b48cd99..03ca543c8 100644
--- a/ietf/review/mailarch.py
+++ b/ietf/review/mailarch.py
@@ -4,6 +4,7 @@
 import datetime, tarfile, mailbox, tempfile, hashlib, base64, email.utils
 import urllib
 import urllib2, contextlib
+import re
 
 from django.conf import settings
 
@@ -92,6 +93,9 @@ def retrieve_messages(query_data_url):
     with contextlib.closing(urllib2.urlopen(query_data_url, timeout=15)) as fileobj:
         content_type = fileobj.info()["Content-type"]
         if not content_type.startswith("application/x-tar"):
+            if content_type.startswith("text/html"):
+                if not re.search("no-results", fileobj.read(20000)) is None:
+                    raise Exception("NONE")
             raise Exception("Export failed - this usually means no matches were found")
 
         with tarfile.open(fileobj=fileobj, mode='r|*') as tar: