~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/apps/errors.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# GNU Enterprise Common - Base exception classes
 
2
#
 
3
# Copyright 2001-2005 Free Software Foundation
 
4
#
 
5
# This file is part of GNU Enterprise
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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.
 
21
#
 
22
# $Id: errors.py 6851 2005-01-03 20:59:28Z jcater $
 
23
 
 
24
import sys
 
25
import traceback
 
26
import types
 
27
import string
 
28
import exceptions
 
29
 
 
30
from gnue.common.apps import i18n
 
31
 
 
32
# =============================================================================
 
33
# New basic exception class. Python's standard exception class cannot handle
 
34
# unicode messages.
 
35
# =============================================================================
 
36
 
 
37
class gException (Exception):
 
38
  """
 
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
 
42
  class.
 
43
  """
 
44
  def __init__ (self, message, group = 'system'):
 
45
    self.message = message
 
46
    self.group   = group
 
47
    self.name    = None
 
48
    self.detail  = None
 
49
    exceptions.Exception.__init__ (self, o(message))
 
50
 
 
51
  # ---------------------------------------------------------------------------
 
52
  # Get the type of the exception
 
53
  # ---------------------------------------------------------------------------
 
54
 
 
55
  def getGroup (self):
 
56
    """
 
57
    This function returns the group of the exception.
 
58
    @return: group of the exception as string
 
59
    """
 
60
    return self.group
 
61
 
 
62
 
 
63
  # ---------------------------------------------------------------------------
 
64
  # Return the name of the exception
 
65
  # ---------------------------------------------------------------------------
 
66
 
 
67
  def getName (self, aType = None):
 
68
    """
 
69
    This function returns the name of the exception (i.e. 'FooBarError')
 
70
    @return: name of the exception as unicode string
 
71
    """
 
72
    rep = self.name or "%s" % (sys.exc_info () [0] or aType)
 
73
    return self._fmtUnicode (rep.split ('.') [-1])
 
74
 
 
75
 
 
76
  # ---------------------------------------------------------------------------
 
77
  # Get the detail of an exception
 
78
  # ---------------------------------------------------------------------------
 
79
 
 
80
  def getDetail (self, count = None, type = None, value = None, trace = None):
 
81
    """
 
82
    This function returns the exception's detail which is a traceback for
 
83
    gException instances.
 
84
    @param count: number of lines to skip in the traceback
 
85
    @return: unicode string with the exception's traceback
 
86
    """
 
87
    if self.detail is not None:
 
88
      return self._fmtUnicode (self.detail, i18n.getencoding ())
 
89
 
 
90
    if sys.exc_info () == (None, None, None):
 
91
      tStack = traceback.format_exception (type, value, trace)
 
92
    else:
 
93
      tStack = traceback.format_exception (*sys.exc_info ())
 
94
    if count is not None:
 
95
      del tStack [1:count + 1]
 
96
    return self._fmtUnicode ("%s" % string.join (tStack), i18n.getencoding ())
 
97
 
 
98
 
 
99
  # ---------------------------------------------------------------------------
 
100
  # Get the message of an exception
 
101
  # ---------------------------------------------------------------------------
 
102
 
 
103
  def getMessage (self):
 
104
    """
 
105
    This function returns the message of an exception
 
106
    @return: unicode string with the message of the exception
 
107
    """
 
108
    return self._fmtUnicode (self.message)
 
109
 
 
110
 
 
111
  # ---------------------------------------------------------------------------
 
112
  # Make sure a given text is a unicode string
 
113
  # ---------------------------------------------------------------------------
 
114
 
 
115
  def _fmtUnicode (self, text, encoding = None):
 
116
    """
 
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.
 
122
    """
 
123
    if isinstance (text, types.UnicodeType):
 
124
      return text
 
125
    else:
 
126
      if encoding is not None:
 
127
        return unicode (text, encoding, 'replace')
 
128
      else:
 
129
        return unicode (text, errors = 'replace')
 
130
 
 
131
 
 
132
# =============================================================================
 
133
# System Error
 
134
# =============================================================================
 
135
 
 
136
class SystemError (gException):
 
137
  """
 
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 :)
 
140
  """
 
141
  def __init__ (self, message):
 
142
    gException.__init__ (self, message, 'system')
 
143
 
 
144
 
 
145
# =============================================================================
 
146
# Administrative Errors
 
147
# =============================================================================
 
148
 
 
149
class AdminError (gException):
 
150
  """
 
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.
 
154
  """
 
155
  def __init__ (self, message):
 
156
    gException.__init__ (self, message, 'admin')
 
157
 
 
158
 
 
159
# =============================================================================
 
160
# Application Errors
 
161
# =============================================================================
 
162
 
 
163
class ApplicationError (gException):
 
164
  """
 
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.
 
167
  """
 
168
  def __init__ (self, message):
 
169
    gException.__init__ (self, message, 'application')
 
170
 
 
171
 
 
172
# =============================================================================
 
173
# User Errors
 
174
# =============================================================================
 
175
 
 
176
class UserError (gException):
 
177
  """
 
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
 
182
  """
 
183
  def __init__ (self, message):
 
184
    gException.__init__ (self, message, 'user')
 
185
 
 
186
 
 
187
# =============================================================================
 
188
# Exceptions raised on a remote site/process
 
189
# =============================================================================
 
190
 
 
191
class RemoteError (gException):
 
192
  """
 
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.
 
196
  """
 
197
  def __init__ (self, group, name, message, detail):
 
198
    gException.__init__ (self, message)
 
199
    self.group  = group
 
200
    self.name   = name
 
201
    self.detail = detail
 
202
 
 
203
 
 
204
# -----------------------------------------------------------------------------
 
205
# Get a tuple (type, name, message, detail) for the last exception raised
 
206
# -----------------------------------------------------------------------------
 
207
 
 
208
def getException (count = None, aType = None, aValue = None, aTrace = None):
 
209
  """
 
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.
 
215
  """
 
216
  (sType, sValue, sTrace) = sys.exc_info ()
 
217
  aType  = aType  or sType
 
218
  aValue = aValue or sValue
 
219
  aTrace = aTrace or sTrace
 
220
 
 
221
  if isinstance (aValue, gException):
 
222
    return (aValue.getGroup (), aValue.getName (), aValue.getMessage (),
 
223
            aValue.getDetail (count, aType, aValue, aTrace))
 
224
  else:
 
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]
 
230
 
 
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)
 
237
 
 
238
 
 
239
# =============================================================================
 
240
# Code executed once per module load
 
241
# =============================================================================
 
242
 
 
243
import __builtin__
 
244
__builtin__.__dict__['gException'] = gException