Back to index

moin  1.9.0~rc2
error.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """ MoinMoin errors
00003 
00004     Supply Error class and sub classes used to raise various errors
00005 
00006     @copyright: 2004-2005 Nir Soffer <nirs@freeshell.org>
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 import sys
00011 
00012 from MoinMoin import config
00013 
00014 class Error(Exception):
00015     """ Base class for moin moin errors
00016 
00017     Use this class when you raise errors or create sub classes that
00018     may be used to display non ASCII error message.
00019 
00020     Standard errors work safely only with strings using ascii or
00021     unicode. This class can be used safely with both strings using
00022     config.charset and unicode.
00023 
00024     You can init this class with either unicode or string using
00025     config.charset encoding. On output, the class will convert the string
00026     to unicode or the unicode to string, using config.charset.
00027 
00028     When you want to render an error, use unicode() or str() as needed.
00029     """
00030 
00031     def __init__(self, message):
00032         """ Initialize an error, decode if needed
00033 
00034         @param message: unicode, str or object that support __unicode__
00035             and __str__. __str__ should use config.charset.
00036         """
00037         self.message = message
00038 
00039     def __unicode__(self):
00040         """ Return unicode error message """
00041         if isinstance(self.message, str):
00042             return unicode(self.message, config.charset)
00043         else:
00044             return unicode(self.message)
00045 
00046     def __str__(self):
00047         """ Return encoded message """
00048         if isinstance(self.message, unicode):
00049             return self.message.encode(config.charset)
00050         else:
00051             return str(self.message)
00052 
00053     def __getitem__(self, item):
00054         """ Make it possible to access attributes like a dict """
00055         return getattr(self, item)
00056 
00057 
00058 class CompositeError(Error):
00059     """ Base class for exceptions containing an exception
00060 
00061     Do not use this class but its more specific sub classes.
00062 
00063     Useful for hiding low level error inside high level user error,
00064     while keeping the inner error information for debugging.
00065 
00066     Example::
00067 
00068         class InternalError(CompositeError):
00069             ''' Raise for internal errors '''
00070 
00071         try:
00072             # code that might fail...
00073         except HairyLowLevelError:
00074             raise InternalError("Sorry, internal error occurred")
00075 
00076     When showing a traceback, both InternalError traceback and
00077     HairyLowLevelError traceback are available.
00078     """
00079 
00080     def __init__(self, message):
00081         """ Save system exception info before this exception is raised """
00082         Error.__init__(self, message)
00083         self.innerException = sys.exc_info()
00084 
00085     def exceptions(self):
00086         """ Return a list of all inner exceptions """
00087         all = [self.innerException]
00088         while 1:
00089             lastException = all[-1][1]
00090             try:
00091                 all.append(lastException.innerException)
00092             except AttributeError:
00093                 break
00094         return all
00095 
00096 class FatalError(CompositeError):
00097     """ Base class for fatal error we can't handle
00098 
00099     Do not use this class but its more specific sub classes.
00100     """
00101 
00102 class ConfigurationError(FatalError):
00103     """ Raise when fatal misconfiguration is found """
00104 
00105 class InternalError(FatalError):
00106     """ Raise when internal fatal error is found """
00107 
00108 class NoConfigMatchedError(Exception):
00109     """ we didn't find a configuration for this URL """
00110     pass
00111 
00112 class ConvertError(FatalError):
00113     """ Raise when html to storage format (e.g. 'wiki') conversion fails """
00114     name = "MoinMoin Convert Error"
00115 
00116