Repository: psss/did

Issue:
Traceback in github plugin with wrong user
Asking for issues created by non-existing user in github plugin cause traceback. It would be good to print warning / error and continue.

DEBUG  GitHub query: https://api.github.com/search/issues?q=author:nonexistantuser+created:2016-04-01..2016-06-24
 DEBUG: HTTP Error 422: Unprocessable Entity
Traceback (most recent call last):
  File "/bin/did", line 42, in <module>
    did.cli.main()
  File "/usr/lib/python2.7/site-packages/did/cli.py", line 190, in main
    user_stats.check()
  File "/usr/lib/python2.7/site-packages/did/stats.py", line 134, in check
    stat.check()
  File "/usr/lib/python2.7/site-packages/did/stats.py", line 134, in check
    stat.check()
  File "/usr/lib/python2.7/site-packages/did/stats.py", line 79, in check
    self.fetch()
  File "/usr/lib/python2.7/site-packages/did/plugins/github.py", line 100, in fetch
    Issue(issue) for issue in self.parent.github.search(query)]
  File "/usr/lib/python2.7/site-packages/did/plugins/github.py", line 60, in search
    "GitHub search on {0} failed.".format(self.url))
did.base.ReportError: GitHub search on https://api.github.com failed.

Discussion:

Solution patch:
diff --git a/did/plugins/github.py b/did/plugins/github.py
index 20f1f36..e3d8eb8 100644
--- a/did/plugins/github.py
+++ b/did/plugins/github.py
@@ -176,9 +176,20 @@ class GitHub():
             # Fetch the query
             log.debug("GitHub query: %s", url)
             response = self.request(url)
+            if not response.ok:
+                try:
+                    error = json.loads(response.text)["errors"][0]["message"]
+                except KeyError:
+                    error = "unknown"
+                raise ReportError(
+                    f"Failed to fetch GitHub data at '{url}'. "
+                    f"The reason was '{response.reason}' "
+                    f"and the error was '{error}'.")
+            log.data(pretty(response.text))
             # Parse fetched json data
             try:
                 data = json.loads(response.text)["items"]
+                log.debug(data)
                 result.extend(data)
             except requests.exceptions.JSONDecodeError as error:
                 log.debug(error)


Test patch:
diff --git a/tests/plugins/test_github.py b/tests/plugins/test_github.py
index 457b8d9..1fb42ef 100644
--- a/tests/plugins/test_github.py
+++ b/tests/plugins/test_github.py
@@ -122,6 +122,25 @@ def test_github_missing_url(caplog: LogCaptureFixture):
         assert "Skipping section gh due to error: No github url set" in caplog.text
 
 
+def test_github_wrong_user(caplog: LogCaptureFixture):
+    """
+    Test non existing user error reporting
+    @see: https://github.com/psss/did/issues/101
+    """
+    did.base.Config("""
+                    [general]
+                    email = "Petr Splichal" <psplicha@redhat.com>
+
+                    [gh]
+                    type = github
+                    url = https://api.github.com/
+                    login = nonexistantuser
+                    """)
+    with caplog.at_level(logging.ERROR):
+        did.cli.main(INTERVAL)
+        assert "The listed users cannot be searched" in caplog.text
+
+
 def test_github_unicode():
     """ Created issues with Unicode characters """
     did.base.Config("[gh]\ntype = github\nurl = https://api.github.com/")