1
# GNU Enterprise Common - Base exception classes
3
# Copyright 2001-2005 Free Software Foundation
5
# This file is part of GNU Enterprise
7
# GNU Enterprise is free software; you can redistribute it
8
# and/or modify it under the terms of the GNU General Public
9
# License as published by the Free Software Foundation; either
10
# version 2, or (at your option) any later version.
12
# GNU Enterprise is distributed in the hope that it will be
13
# useful, but WITHOUT ANY WARRANTY; without even the implied
14
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
# PURPOSE. See the GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public
18
# License along with program; see the file COPYING. If not,
19
# write to the Free Software Foundation, Inc., 59 Temple Place
20
# - Suite 330, Boston, MA 02111-1307, USA.
22
# $Id: errors.py 6851 2005-01-03 20:59:28Z jcater $
30
from gnue.common.apps import i18n
32
# =============================================================================
33
# New basic exception class. Python's standard exception class cannot handle
35
# =============================================================================
37
class gException (Exception):
39
The same as the builtin python Exception, but can handle messages that are
40
unicode strings. This exception is available as the builtin class
41
"gException". All other user-defined exceptions should be derived from this
44
def __init__ (self, message, group = 'system'):
45
self.message = message
49
exceptions.Exception.__init__ (self, o(message))
51
# ---------------------------------------------------------------------------
52
# Get the type of the exception
53
# ---------------------------------------------------------------------------
57
This function returns the group of the exception.
58
@return: group of the exception as string
63
# ---------------------------------------------------------------------------
64
# Return the name of the exception
65
# ---------------------------------------------------------------------------
67
def getName (self, aType = None):
69
This function returns the name of the exception (i.e. 'FooBarError')
70
@return: name of the exception as unicode string
72
rep = self.name or "%s" % (sys.exc_info () [0] or aType)
73
return self._fmtUnicode (rep.split ('.') [-1])
76
# ---------------------------------------------------------------------------
77
# Get the detail of an exception
78
# ---------------------------------------------------------------------------
80
def getDetail (self, count = None, type = None, value = None, trace = None):
82
This function returns the exception's detail which is a traceback for
84
@param count: number of lines to skip in the traceback
85
@return: unicode string with the exception's traceback
87
if self.detail is not None:
88
return self._fmtUnicode (self.detail, i18n.getencoding ())
90
if sys.exc_info () == (None, None, None):
91
tStack = traceback.format_exception (type, value, trace)
93
tStack = traceback.format_exception (*sys.exc_info ())
95
del tStack [1:count + 1]
96
return self._fmtUnicode ("%s" % string.join (tStack), i18n.getencoding ())
99
# ---------------------------------------------------------------------------
100
# Get the message of an exception
101
# ---------------------------------------------------------------------------
103
def getMessage (self):
105
This function returns the message of an exception
106
@return: unicode string with the message of the exception
108
return self._fmtUnicode (self.message)
111
# ---------------------------------------------------------------------------
112
# Make sure a given text is a unicode string
113
# ---------------------------------------------------------------------------
115
def _fmtUnicode (self, text, encoding = None):
117
This function returns a given text as unicode string using an optional
118
encoding or the system's default encoding.
119
@param text: the string to be encoded. If this string is already unicode no
120
modification will take place.
121
@return: unicode representation of @text.
123
if isinstance (text, types.UnicodeType):
126
if encoding is not None:
127
return unicode (text, encoding, 'replace')
129
return unicode (text, errors = 'replace')
132
# =============================================================================
134
# =============================================================================
136
class SystemError (gException):
138
This exception class should be used for exceptions indicating a bug in GNUe.
139
Whenever such an exception is raised, one have found such a bug :)
141
def __init__ (self, message):
142
gException.__init__ (self, message, 'system')
145
# =============================================================================
146
# Administrative Errors
147
# =============================================================================
149
class AdminError (gException):
151
This exception class should be used for exceptions indicating a
152
misconfiguration in a widest sense. This could be a missing module for a
153
dbdriver as well as an 'out of disk space' error.
155
def __init__ (self, message):
156
gException.__init__ (self, message, 'admin')
159
# =============================================================================
161
# =============================================================================
163
class ApplicationError (gException):
165
This class should be used for errors caused by applications like a corrupt
166
trigger code, or a misformed xml-file and so on.
168
def __init__ (self, message):
169
gException.__init__ (self, message, 'application')
172
# =============================================================================
174
# =============================================================================
176
class UserError (gException):
178
This class should be used for exceptions where a user did something wrong, or
179
a situation has occured which isn't dramatic, but the user has to be informed
180
of. Example: wrong password or the user has entered non-numeric data into a
181
numeric field, and so on
183
def __init__ (self, message):
184
gException.__init__ (self, message, 'user')
187
# =============================================================================
188
# Exceptions raised on a remote site/process
189
# =============================================================================
191
class RemoteError (gException):
193
This class is used for transporting an exception raised at a remote point.
194
Once it has been created it never changes it's contents. A remote error
195
usually contains System-, Admin- or User-Errors.
197
def __init__ (self, group, name, message, detail):
198
gException.__init__ (self, message)
204
# -----------------------------------------------------------------------------
205
# Get a tuple (type, name, message, detail) for the last exception raised
206
# -----------------------------------------------------------------------------
208
def getException (count = None, aType = None, aValue = None, aTrace = None):
210
This function creates a tuple (type, name, message, detail) for the last
211
exception raised. The optional parameter determines the number of lines
212
skipped from the detail traceback.
213
@param count: number of lines to skip in the traceback
214
@returns: tuple with type, name, message and detail of the last exception.
216
(sType, sValue, sTrace) = sys.exc_info ()
217
aType = aType or sType
218
aValue = aValue or sValue
219
aTrace = aTrace or sTrace
221
if isinstance (aValue, gException):
222
return (aValue.getGroup (), aValue.getName (), aValue.getMessage (),
223
aValue.getDetail (count, aType, aValue, aTrace))
225
# Exception was not a descendant of gException, so we construct the tuple
226
# from the exception information
227
lines = traceback.format_exception (aType, aValue, aTrace)
228
if count is not None:
229
del lines [1:count + 1]
231
name = unicode ("%s" % aType, i18n.getencoding ()).split ('.') [-1]
232
message = unicode ("%s" % aValue, i18n.getencoding ())
233
detail = string.join (lines)
234
if isinstance (detail, types.StringType):
235
detail = unicode (detail, i18n.getencoding ())
236
return ('system', name, message, detail)
239
# =============================================================================
240
# Code executed once per module load
241
# =============================================================================
244
__builtin__.__dict__['gException'] = gException