From 3edfb7d5c337e09208029f471906f16db52aa069 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 26 Sep 2013 19:48:48 +0800 Subject: [PATCH] Correctly deal with API errors. --- script/lib/github.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/script/lib/github.py b/script/lib/github.py index 3aa75c6..ba6ea86 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): -- 2.7.4