From: Cheng Zhao Date: Thu, 26 Sep 2013 11:48:48 +0000 (+0800) Subject: Correctly deal with API errors. X-Git-Tag: v0.5.2~7^2~9 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=3edfb7d5c337e09208029f471906f16db52aa069;p=electron.git Correctly deal with API errors. --- diff --git a/script/lib/github.py b/script/lib/github.py index 3aa75c62..ba6ea86c 100644 --- a/script/lib/github.py +++ b/script/lib/github.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import json import requests GITHUB_URL = 'https://api.github.com' @@ -15,10 +16,16 @@ class GitHub: url = '%s%s' % (GITHUB_URL, path) if not 'headers' in kw: kw['headers'] = dict() - kw['headers']['Authorization'] = self._authorization - kw['headers']['Accept'] = 'application/vnd.github.manifold-preview' + headers = kw['headers'] + headers['Authorization'] = self._authorization + headers['Accept'] = 'application/vnd.github.manifold-preview' + if 'data' in kw: + kw['data'] = json.dumps(kw['data']) - return getattr(requests, method)(url, **kw) + r = getattr(requests, method)(url, **kw).json() + if 'message' in r: + raise Exception(json.dumps(r, indent=2, separators=(',', ': '))) + return r class _Executable: @@ -28,7 +35,7 @@ class _Executable: self._path = path def __call__(self, **kw): - return self._gh._http(self._method, self._path, **kw).json() + return self._gh._http(self._method, self._path, **kw) class _Callable(object):