Raise exception on API errors
[mw] / src / mw / api.py
index 8961f5d8a00d14cc408146f237c5c44570de92c4..09467824b1e766695e3a58bb0a5b38cc79351ae7 100644 (file)
@@ -1,6 +1,6 @@
 ###
 # mw - VCS-like nonsense for MediaWiki websites
-# Copyright (C) 2009  Ian Weller <ian@ianweller.org>
+# Copyright (C) 2010  Ian Weller <ian@ianweller.org>
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -23,7 +23,9 @@ from StringIO import StringIO
 import urllib
 import urllib2
 
+
 class API(object):
+
     def __init__(self, api_url):
         self.api_url = api_url
         self.cookiejar = cookielib.CookieJar()
@@ -42,7 +44,10 @@ class API(object):
             data = gzipper.read()
         else:
             data = response.read()
-        return json.loads(data)
+        the_data = json.loads(data)
+        if 'error' in the_data.keys():
+            raise APIError(the_data['error']['info'])
+        return
 
     def limits(self, low, high):
         if self._high_limits == None:
@@ -57,12 +62,22 @@ class API(object):
             return low
 
 
+class APIError(Exception):
+
+    def __init__(self, info):
+        self.info = info
+
+    def __str__(self):
+        return self.info
+
+
 def pagename_to_filename(name):
-    name.replace(' ', '_')
-    name.replace('/', '!')
+    name = name.replace(' ', '_')
+    name = name.replace('/', '!')
     return name
 
+
 def filename_to_pagename(name):
-    name.replace('!', '/')
-    name.replace('_', ' ')
+    name = name.replace('!', '/')
+    name = name.replace('_', ' ')
     return name

Benjamin Mako Hill || Want to submit a patch?