def __call__(self): self.request.response['CONTENT-TYPE'] = 'application/json' if IUnauthorized.providedBy(self.context): # FIXME: here we are hacking the Zope2.App.startup.ZPublisherExceptionHook.__call__ # We should have been called from there. Problem is that it re-raises # unauthorised exception and therefore makes it impossible to # override response body. # Returned status code is not entirely correct, but at least body # content is json import sys caller = sys._getframe().f_back # replace exception class with another that is not re-raised caller.f_locals.update({'t': Forbidden}) import ctypes # call python to turn locals dict in other frame into fast # variables ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(caller), ctypes.c_int(0)) if getattr(self.__parent__, 'errors', None): return json.dumps({'errors': [self.__parent__.errors]}) elif self.context.message: # assume standard exception return json.dumps({'errors': [{'title': self.context.message}]}) else: return json.dumps({ 'errors': [{ 'title': 'Sorry, I have no meaningful error message available' }] })
def handleException(self, exc_info): """Handle exceptions that occur during processing.""" type, value = exc_info[:2] content = "".join(traceback.format_tb(exc_info[-1])) if IUnauthorized.providedBy(value): self.setStatus(401) self.setBody("") #XXXself._body = "" #self._updateContentLength() return if not isinstance(value, Fault): value = ZSI.FaultFromException(u"%s : %s" % (value, content), 0) self.setStatus(500) self.setBody(value)
def __call__(self): self.request.response['CONTENT-TYPE'] = 'application/json' if IUnauthorized.providedBy(self.context): # FIXME: here we are hacking the Zope2.App.startup.ZPublisherExceptionHook.__call__ # We should have been called from there. Problem is that it re-raises # unauthorised exception and therefore makes it impossible to # override response body. # Returned status code is not entirely correct, but at least body # content is json import sys caller = sys._getframe().f_back # replace exception class with another that is not re-raised caller.f_locals.update({'t': Forbidden}) import ctypes # call python to turn locals dict in other frame into fast # variables ctypes.pythonapi.PyFrame_LocalsToFast( ctypes.py_object(caller), ctypes.c_int(0)) if getattr(self.__parent__, 'errors', None): return json.dumps({ 'errors': [ self.__parent__.errors ] }) elif self.context.message: # assume standard exception return json.dumps({ 'errors': [{ 'title': self.context.message }] }) else: return json.dumps({ 'errors': [{ 'title': 'Sorry, I have no meaningful error message available' }] })