Пример #1
0
def transaction_pubevents(request, response, tm=transaction.manager):
    try:
        setDefaultSkin(request)
        newInteraction()
        tm.begin()
        notify(pubevents.PubStart(request))

        yield

        notify(pubevents.PubBeforeCommit(request))
        if tm.isDoomed():
            tm.abort()
        else:
            tm.commit()
        notify(pubevents.PubSuccess(request))
    except Exception as exc:
        # Normalize HTTP exceptions
        # (For example turn zope.publisher NotFound into zExceptions NotFound)
        exc_type, _ = upgradeException(exc.__class__, None)
        if not isinstance(exc, exc_type):
            exc = exc_type(str(exc))

        # Create new exc_info with the upgraded exception.
        exc_info = (exc_type, exc, sys.exc_info()[2])

        try:
            # Raise exception from app if handle-errors is False
            # (set by zope.testbrowser in some cases)
            if request.environ.get('x-wsgiorg.throw_errors', False):
                reraise(*exc_info)

            if isinstance(exc, Unauthorized):
                # _unauthorized modifies the response in-place. If this hook
                # is used, an exception view for Unauthorized has to merge
                # the state of the response and the exception instance.
                exc.setRealm(response.realm)
                response._unauthorized()
                response.setStatus(exc.getStatus())

            # Handle exception view
            exc_view_created = _exc_view_created_response(
                exc, request, response)

            notify(
                pubevents.PubBeforeAbort(request, exc_info,
                                         request.supports_retry()))
            tm.abort()
            notify(
                pubevents.PubFailure(request, exc_info,
                                     request.supports_retry()))

            if not (exc_view_created or isinstance(exc, Unauthorized)):
                reraise(*exc_info)
        finally:
            # Avoid traceback / exception reference cycle.
            del exc, exc_info
    finally:
        endInteraction()
Пример #2
0
def transaction_pubevents(request, response, tm=transaction.manager):
    try:
        setDefaultSkin(request)
        newInteraction()
        tm.begin()
        notify(pubevents.PubStart(request))

        yield

        notify(pubevents.PubBeforeCommit(request))
        if tm.isDoomed():
            tm.abort()
        else:
            tm.commit()
        notify(pubevents.PubSuccess(request))
    except Exception as exc:
        # Normalize HTTP exceptions
        # (For example turn zope.publisher NotFound into zExceptions NotFound)
        exc_type, _ = upgradeException(exc.__class__, None)
        if not isinstance(exc, exc_type):
            exc = exc_type(str(exc))

        # Create new exc_info with the upgraded exception.
        exc_info = (exc_type, exc, sys.exc_info()[2])

        try:
            # Raise exception from app if handle-errors is False
            # (set by zope.testbrowser in some cases)
            if request.environ.get('x-wsgiorg.throw_errors', False):
                reraise(*exc_info)

            if isinstance(exc, Unauthorized):
                # _unauthorized modifies the response in-place. If this hook
                # is used, an exception view for Unauthorized has to merge
                # the state of the response and the exception instance.
                exc.setRealm(response.realm)
                response._unauthorized()
                response.setStatus(exc.getStatus())

            # Handle exception view
            exc_view_created = _exc_view_created_response(
                exc, request, response)

            notify(pubevents.PubBeforeAbort(
                request, exc_info, request.supports_retry()))
            tm.abort()
            notify(pubevents.PubFailure(
                request, exc_info, request.supports_retry()))

            if not (exc_view_created or isinstance(exc, Unauthorized)):
                reraise(*exc_info)
        finally:
            # Avoid traceback / exception reference cycle.
            del exc, exc_info
    finally:
        endInteraction()
Пример #3
0
def _publish_response(request, response, module_info, _publish=publish):
    try:
        with transaction_pubevents(request):
            response = _publish(request, module_info)
    except Exception as exc:
        # Normalize HTTP exceptions
        # (For example turn zope.publisher NotFound into zExceptions NotFound)
        t, v = upgradeException(exc.__class__, None)
        if not isinstance(exc, t):
            exc = t(str(exc))

        if isinstance(exc, Unauthorized):
            # _unauthorized modifies the response in-place. If this hook
            # is used, an exception view for Unauthorized has to merge
            # the state of the response and the exception instance.
            exc.setRealm(response.realm)
            response._unauthorized()
            response.setStatus(exc.getStatus())

        view = queryMultiAdapter((exc, request), name=u'index.html')
        if view is not None:
            # Wrap the view in the context in which the exception happened.
            parents = request.get('PARENTS')
            if parents:
                view.__parent__ = parents[0]

            # Set status and headers from the exception on the response,
            # which would usually happen while calling the exception
            # with the (environ, start_response) WSGI tuple.
            response.setStatus(exc.__class__)
            if hasattr(exc, 'headers'):
                for key, value in exc.headers.items():
                    response.setHeader(key, value)

            # Set the response body to the result of calling the view.
            response.setBody(view())
            return response

        if isinstance(exc, Unauthorized):
            return response

        # Reraise the exception, preserving the original traceback
        reraise(exc.__class__, exc, sys.exc_info()[2])

    return response
Пример #4
0
def _publish_response(request, response, module_info, _publish=publish):
    try:
        with transaction_pubevents(request):
            response = _publish(request, module_info)
    except Exception as exc:
        # Normalize HTTP exceptions
        # (For example turn zope.publisher NotFound into zExceptions NotFound)
        t, v = upgradeException(exc.__class__, None)
        if t is not exc.__class__:
            exc = t(str(exc))

        # This should happen inside zExceptions, but the realm is only
        # defined on the premade response or in the module_info and
        # can be changed during publishing.
        if isinstance(exc, Unauthorized):
            exc.setRealm(response.realm)

        view = queryMultiAdapter((exc, request), name=u'index.html')
        if view is not None:
            # Wrap the view in the context in which the exception happened.
            parents = request.get('PARENTS')
            if parents:
                view.__parent__ = parents[0]

            # Set status and headers from the exception on the response,
            # which would usually happen while calling the exception
            # with the (environ, start_response) WSGI tuple.
            response.setStatus(exc.__class__)
            if hasattr(exc, 'headers'):
                for key, value in exc.headers.items():
                    response.setHeader(key, value)

            # Set the response body to the result of calling the view.
            response.setBody(view())
            return response

        raise exc

    return response
Пример #5
0
    def render(self,md):
        expr = self.expr
        if expr is None:
            t = convertExceptionType(self.__name__)
            if t is None:
                t = RuntimeError
        else:
            try:
                t = expr.eval(md)
            except:
                t = convertExceptionType(self.__name__)
                if t is None:
                    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
Пример #6
0
    def render(self, md):
        expr = self.expr
        if expr is None:
            t = convertExceptionType(self.__name__)
            if t is None:
                t = RuntimeError
        else:
            try:
                t = expr.eval(md)
            except:
                t = convertExceptionType(self.__name__)
                if t is None:
                    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
Пример #7
0
 def _callFUT(self, t, v):
     from zExceptions import upgradeException
     return upgradeException(t, v)
Пример #8
0
def transaction_pubevents(request, response, tm=transaction.manager):
    try:
        setDefaultSkin(request)
        newInteraction()
        tm.begin()
        notify(pubevents.PubStart(request))

        yield

        notify(pubevents.PubBeforeCommit(request))
        if tm.isDoomed():
            tm.abort()
        else:
            tm.commit()
        notify(pubevents.PubSuccess(request))
    except Exception as exc:
        # Normalize HTTP exceptions
        # (For example turn zope.publisher NotFound into zExceptions NotFound)
        exc_type, _ = upgradeException(exc.__class__, None)
        if not isinstance(exc, exc_type):
            exc = exc_type(str(exc))

        # Create new exc_info with the upgraded exception.
        exc_info = (exc_type, exc, sys.exc_info()[2])

        try:
            # Raise exception from app if handle-errors is False
            # (set by zope.testbrowser in some cases)
            if request.environ.get('x-wsgiorg.throw_errors', False):
                reraise(*exc_info)

            retry = False
            unauth = False
            debug_exc = getattr(response, 'debug_exceptions', False)

            # If the exception is transient and the request can be retried,
            # shortcut further processing. It makes no sense to have an
            # exception view registered for this type of exception.
            if isinstance(exc, TransientError) and request.supports_retry():
                retry = True
            else:
                # Handle exception view. Make sure an exception view that
                # blows up doesn't leave the user e.g. unable to log in.
                try:
                    exc_view_created = _exc_view_created_response(
                        exc, request, response)
                except Exception:
                    exc_view_created = False

                # _unauthorized modifies the response in-place. If this hook
                # is used, an exception view for Unauthorized has to merge
                # the state of the response and the exception instance.
                if isinstance(exc, Unauthorized):
                    unauth = True
                    exc.setRealm(response.realm)
                    response._unauthorized()
                    response.setStatus(exc.getStatus())

            # Notify subscribers that this request is failing.
            notify(pubevents.PubBeforeAbort(request, exc_info, retry))
            tm.abort()
            notify(pubevents.PubFailure(request, exc_info, retry))

            if retry or \
               (not unauth and (debug_exc or not exc_view_created)):
                reraise(*exc_info)

        finally:
            # Avoid traceback / exception reference cycle.
            del exc, exc_info
    finally:
        endInteraction()
Пример #9
0
 def _callFUT(self, t, v):
     from zExceptions import upgradeException
     return upgradeException(t, v)
Пример #10
0
def transaction_pubevents(request, response, tm=transaction.manager):
    try:
        setDefaultSkin(request)
        newInteraction()
        tm.begin()
        notify(pubevents.PubStart(request))

        yield

        notify(pubevents.PubBeforeCommit(request))
        if tm.isDoomed():
            tm.abort()
        else:
            tm.commit()
        notify(pubevents.PubSuccess(request))
    except Exception as exc:
        # Normalize HTTP exceptions
        # (For example turn zope.publisher NotFound into zExceptions NotFound)
        exc_type, _ = upgradeException(exc.__class__, None)
        if not isinstance(exc, exc_type):
            exc = exc_type(str(exc))

        # Create new exc_info with the upgraded exception.
        exc_info = (exc_type, exc, sys.exc_info()[2])

        try:
            # Raise exception from app if handle-errors is False
            # (set by zope.testbrowser in some cases)
            if request.environ.get('x-wsgiorg.throw_errors', False):
                reraise(*exc_info)

            retry = False
            unauth = False
            debug_exc = getattr(response, 'debug_exceptions', False)

            # If the exception is transient and the request can be retried,
            # shortcut further processing. It makes no sense to have an
            # exception view registered for this type of exception.
            if isinstance(exc, TransientError) and request.supports_retry():
                retry = True
            else:
                # Handle exception view. Make sure an exception view that
                # blows up doesn't leave the user e.g. unable to log in.
                try:
                    exc_view_created = _exc_view_created_response(
                        exc, request, response)
                except Exception:
                    exc_view_created = False

                # _unauthorized modifies the response in-place. If this hook
                # is used, an exception view for Unauthorized has to merge
                # the state of the response and the exception instance.
                if isinstance(exc, Unauthorized):
                    unauth = True
                    exc.setRealm(response.realm)
                    response._unauthorized()
                    response.setStatus(exc.getStatus())

            # Notify subscribers that this request is failing.
            notify(pubevents.PubBeforeAbort(request, exc_info, retry))
            tm.abort()
            notify(pubevents.PubFailure(request, exc_info, retry))

            if retry or \
               (not unauth and (debug_exc or not exc_view_created)):
                reraise(*exc_info)

        finally:
            # Avoid traceback / exception reference cycle.
            del exc, exc_info
    finally:
        endInteraction()