Package muntjac :: Package terminal :: Module sys_error
[hide private]
[frames] | no frames]

Source Code for Module muntjac.terminal.sys_error

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  import sys 
 17  import traceback 
 18   
 19  try: 
 20      from cStringIO import StringIO 
 21  except ImportError, e: 
 22      from StringIO import StringIO 
 23   
 24  from muntjac.terminal.error_message import IErrorMessage 
 25   
 26   
27 -class SysError(RuntimeError, IErrorMessage):
28 """C{SystemError} is a runtime exception caused by error in 29 system. The system error can be shown to the user as it implements 30 C{IErrorMessage} interface, but contains technical information 31 such as stack trace and exception. 32 33 SystemError does not support HTML in error messages or stack traces. 34 If HTML messages are required, use {@link UserError} or a custom 35 implementation of L{ErrorMessage}. 36 37 @author: Vaadin Ltd. 38 @author: Richard Lincoln 39 @version: 1.1.2 40 """ 41
42 - def __init__(self, *args):
43 """Constructor for SystemError with error message and/or causing 44 exception specified. 45 46 @param args: tuple of the form 47 - () 48 - (message) 49 1. the textual error description. 50 - (message, cause) 51 1. the textual error description. 52 2. the throwable causing the system error. 53 - (cause) 54 1. the throwable causing the system error. 55 """ 56 # The cause of the system error. 57 self._cause = None 58 59 nargs = len(args) 60 if nargs == 0: 61 super(SysError, self).__init__() 62 elif nargs == 1: 63 if isinstance(args[0], Exception): 64 self._cause = args[0] 65 super(SysError, self).__init__() 66 else: 67 super(SysError, self).__init__(args[0]) 68 elif nargs == 2: 69 message, cause = args 70 super(SysError, self).__init__(message) 71 self._cause = cause 72 else: 73 raise ValueError, ('too many arguments: %d' % nargs)
74 75
76 - def getErrorLevel(self):
77 """@see: L{IErrorMessage.getErrorLevel}""" 78 return IErrorMessage.SYSTEMERROR
79 80
81 - def paint(self, target):
82 """@see: L{IPaintable.paint}""" 83 84 target.startTag('error') 85 target.addAttribute('level', 'system') 86 87 message = self.getHtmlMessage() 88 89 target.addXMLSection('div', message, 90 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd') 91 92 target.endTag('error')
93 94
95 - def getHtmlMessage(self):
96 97 from muntjac.terminal.gwt.server.abstract_application_servlet \ 98 import AbstractApplicationServlet 99 100 sb = StringIO() 101 message = self.message 102 if message is not None: 103 sb.write('<h2>') 104 sb.write(AbstractApplicationServlet.safeEscapeForHtml(message)) 105 sb.write('</h2>') 106 107 # Paint the exception 108 if self._cause is not None: 109 sb.write('<h3>Exception</h3>') 110 buff = StringIO() 111 exc_type, _, exc_traceback = sys.exc_info() 112 traceback.print_exception(exc_type, self._cause, 113 exc_traceback, file=buff) 114 115 sb.write('<pre>') 116 pre = buff.getvalue() 117 sb.write(AbstractApplicationServlet.safeEscapeForHtml(pre)) 118 buff.close() 119 sb.write('</pre>') 120 121 result = sb.getvalue() 122 sb.close() 123 return result
124 125
126 - def getCause(self):
127 """Gets cause for the error. 128 129 @return: the cause. 130 """ 131 return self._cause
132 133
134 - def addListener(self, listener, iface=None):
135 pass
136 137
138 - def addCallback(self, callback, eventType=None, *args):
139 pass
140 141
142 - def removeListener(self, listener, iface=None):
143 pass
144 145
146 - def removeCallback(self, callback, eventType=None):
147 pass
148 149
150 - def requestRepaint(self):
151 pass
152 153
154 - def requestRepaintRequests(self):
155 pass
156 157
158 - def getDebugId(self):
159 return None
160 161
162 - def setDebugId(self, idd):
163 raise NotImplementedError, \ 164 'Setting testing id for this Paintable is not implemented'
165