Commit 462dbb4e authored by Sidnei da Silva's avatar Sidnei da Silva

- Provide a helper function to upgrade a string exception to a real exception.

parent 6a5dfc32
......@@ -26,8 +26,12 @@
__rcs_id__='$Id$'
__version__='$Revision: 1.13 $'[11:-2]
from zExceptions import upgradeException
from DT_Util import parse_params, name_param, render_blocks, str
class InvalidErrorTypeExpression(Exception):
pass
class Raise:
blockContinuations=()
name='raise'
......@@ -44,15 +48,17 @@ class Raise:
expr=self.expr
if expr is None:
t=self.__name__
if t[-5:]=='Error' and __builtins__.has_key(t):
t=__builtins__[t]
else:
try: t=expr.eval(md)
except: t='Invalid Error Type Expression'
except: t=InvalidErrorTypeExpression
try: v=render_blocks(self.section,md)
except: v='Invalid Error Value'
# String Exceptions are deprecated on Python 2.5 and
# plain won't work at all on Python 2.6. So try to upgrade it
# to a real exception.
t, v = upgradeException(t, v)
raise t, v
__call__=render
......@@ -36,7 +36,7 @@ from DocumentTemplate.html_quote import html_quote
from DocumentTemplate.ustr import ustr
from ExtensionClass import Base
from webdav.Resource import Resource
from zExceptions import Redirect, InternalError
from zExceptions import Redirect, upgradeException
from zExceptions.ExceptionFormatter import format_exception
from zope.interface import implements
......@@ -185,15 +185,10 @@ class Item(Base, Resource, CopySource, App.Management.Tabs, Traversable,
error_name = 'Unknown'
if isinstance(error_type, basestring):
# String Exceptions are deprecated on Python 2.5 and
# plain won't work at all on Python 2.6. So upgrade it
# to an InternalError exception but keep the original
# exception in the value.
# plain won't work at all on Python 2.6. So try to upgrade it
# to a real exception.
error_name = error_type
error_type = InternalError
error_value = (error_name, error_value)
warnings.warn('String exceptions are deprecated starting '
'with Python 2.5 and will be removed in a '
'future release', DeprecationWarning)
error_type, error_value = upgradeException(error_type, error_value)
else:
if hasattr(error_type, '__name__'):
error_name = error_type.__name__
......
......@@ -18,12 +18,13 @@ application-specific packages.
$Id$
"""
from unauthorized import Unauthorized
import warnings
from zope.interface import implements
from zope.interface.common.interfaces import IException
from zope.publisher.interfaces import INotFound
from zope.security.interfaces import IForbidden
from zExceptions.unauthorized import Unauthorized
class BadRequest(Exception):
implements(IException)
......@@ -42,3 +43,29 @@ class MethodNotAllowed(Exception):
class Redirect(Exception):
pass
def upgradeException(t, v):
# If a string exception is found, convert it to an equivalent
# exception defined either in builtins or zExceptions. If none of
# that works, tehn convert it to an InternalError and keep the
# original exception name as part of the exception value.
import zExceptions
if not isinstance(t, basestring):
return t, v
warnings.warn('String exceptions are deprecated starting '
'with Python 2.5 and will be removed in a '
'future release', DeprecationWarning)
n = None
if __builtins__.has_key(t):
n = __builtins__[t]
elif hasattr(zExceptions, t):
n = getattr(zExceptions, t)
if n is not None and issubclass(n, Exception):
t = n
else:
v = t, v
t = InternalError
return t, v
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment