|
| 1 | +""" |
| 2 | +Global wikia exception and warning classes. |
| 3 | +""" |
| 4 | + |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +ODD_ERROR_MESSAGE = "This shouldn't happen. Please report on GitHub: https://github.com/NotThatSiri/DLD-Bot" |
| 9 | +class WikiaException(Exception): |
| 10 | + """Base Wikia exception class.""" |
| 11 | + |
| 12 | + def __init__(self, error): |
| 13 | + self.error = error |
| 14 | + |
| 15 | + def __unicode__(self): |
| 16 | + return "An unknown error occured: \"{0}\". Please report it on GitHub!".format(self.error) |
| 17 | + |
| 18 | + if sys.version_info > (3, 0): |
| 19 | + def __str__(self): |
| 20 | + return self.__unicode__() |
| 21 | + |
| 22 | + else: |
| 23 | + def __str__(self): |
| 24 | + return self.__unicode__().encode('utf8') |
| 25 | + |
| 26 | + |
| 27 | +class PageError(WikiaException): |
| 28 | + """Exception raised when no Wikia matched a query.""" |
| 29 | + |
| 30 | + def __init__(self, pageid=None, *args): |
| 31 | + if pageid: |
| 32 | + self.pageid = pageid |
| 33 | + else: |
| 34 | + self.title = args[0] |
| 35 | + |
| 36 | + def __unicode__(self): |
| 37 | + if hasattr(self, 'title'): |
| 38 | + return u"\"{0}\" does not match any pages. Try another query!".format(self.title) |
| 39 | + else: |
| 40 | + return u"Page id \"{0}\" does not match any pages. Try another id!".format(self.pageid) |
| 41 | + |
| 42 | + |
| 43 | +class DisambiguationError(WikiaException): |
| 44 | + """ |
| 45 | + Exception raised when a page resolves to a Disambiguation page. |
| 46 | + The `options` property contains a list of titles |
| 47 | + of Wikia pages that the query may refer to. |
| 48 | + .. note:: `options` does not include titles that do not link to a valid Wikia page. |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__(self, title, may_refer_to): |
| 52 | + self.title = title |
| 53 | + self.options = may_refer_to |
| 54 | + |
| 55 | + def __unicode__(self): |
| 56 | + return u"\"{0}\" may refer to: \n{1}".format(self.title, '\n'.join(self.options)) |
| 57 | + |
| 58 | + |
| 59 | +class RedirectError(WikiaException): |
| 60 | + """Exception raised when a page title unexpectedly resolves to a redirect.""" |
| 61 | + |
| 62 | + def __init__(self, title): |
| 63 | + self.title = title |
| 64 | + |
| 65 | + def __unicode__(self): |
| 66 | + return u"\"{0}\" resulted in a redirect. Set the redirect property to True to allow automatic redirects.".format(self.title) |
| 67 | + |
| 68 | + |
| 69 | +class HTTPTimeoutError(WikiaException): |
| 70 | + """Exception raised when a request to the Mediawiki servers times out.""" |
| 71 | + |
| 72 | + def __init__(self, query): |
| 73 | + self.query = query |
| 74 | + |
| 75 | + def __unicode__(self): |
| 76 | + return u"Searching for \"{0}\" resulted in a timeout. Try again in a few seconds, and make sure you have rate limiting set to True.".format(self.query) |
0 commit comments