1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """
25 Default Error Handler for Biskit classes.
26 """
27
28 import Biskit.tools as T
29 from Biskit.LogFile import ErrLog
30 from Biskit.Errors import HandledError, NormalError, FatalError
31
33 """
34 Default Error Handler for Biskit classes.
35 """
36
38 """
39 @param log: target of error messages, None->StdErr (default: None)
40 @type log: LogFile
41 """
42 self.log = log or ErrLog()
43
44
45 - def fatal( self, message ):
46 """
47 Handle a fatal error (likely a bug), stop program execution.
48
49 @param message: message to be given to user
50 @type message: str
51
52 @raise FatalError:
53 """
54 s = '\nFatal Error: '+str(message)
55 s += '\n\t' + T.lastError() + '\n'
56 s += 'TraceBack: \n' + T.lastErrorTrace() + '\n'
57
58 self.log.add(s)
59 raise FatalError
60
61
62 - def error( self, message ):
63 """
64 Handle a normal error (like non-existing file) that is not
65 necessarily a bug.
66
67 @param message: message to be given to user
68 @type message: str
69
70 @raise NormalError:
71 """
72 s = '\nError: '+str(message)
73 s += '\n\t' + T.lastError()
74 s += '\nTraceBack: \n' + T.lastErrorTrace() + '\n'
75
76 self.log.add(s)
77 raise NormalError
78
79
80 - def warning( self, message, error=1, trace=0 ):
81 """
82 Issue a warning. No exception is raised.
83
84 @param message: message to be given to user
85 @type message: str
86 @param error: report Exception with line (default: 1)
87 @type error: 1||0
88 @param trace: report full back trace to exception (default: 0)
89 @type trace: 1||0
90 """
91 s = '\nWarning (ignored): '+str(message)
92 try:
93 if trace: error = 1
94 if error:
95 s += '\n\t' + T.lastError() + '\n'
96 if trace:
97 s += '\nTraceBack: \n' + T.lastErrorTrace() + '\n'
98 except:
99 pass
100
101 self.log.add(s)
102
103
104
105
106
107
108 import Biskit.test as BT
109
110 -class Test(BT.BiskitTest):
111 """ErrorHandler test"""
112
114 import tempfile
115 self.f_out = tempfile.mktemp( '_test_ErrorHandler' )
116
119
121 """ErrorHandler test"""
122 from Biskit.LogFile import LogFile
123
124 self.err_log = LogFile( self.f_out )
125
126 self.e = ErrorHandler( log=self.err_log )
127 self.e.warning( 'A warning' )
128
129 if self.local:
130 print 'An error log file was written to %s'%self.f_out
131
132 lines = open(self.err_log.fname).readlines()
133 self.assertEquals(lines[-1],'Warning (ignored): A warning\n')
134
135 if __name__ == '__main__':
136
137 BT.localTest(debug=0)
138