-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorhandler.py
More file actions
executable file
·211 lines (170 loc) · 8.51 KB
/
Copy patherrorhandler.py
File metadata and controls
executable file
·211 lines (170 loc) · 8.51 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python
##############################################################################
# Removal of the "__license__" line or content from "__license__", or removal
# of "__author__" in this or any constituent # component or file constitutes a
# violation of the licensing and copyright agreement.
__author__ = "Mike Rightmire"
__copyright__ = "BioCom Software"
__license__ = "Telemend"
__license_file__= "Clause1.PERPETUAL_AND_UNLIMITED_LICENSING_TO_THE_CLIENT.py"
__version__ = "0.9.6.0"
__maintainer__ = "Mike Rightmire"
__status__ = "Development"
##############################################################################
from BiocomCommon.loghandler import log
from handlers import Handlers
import inspect
# import functools
# import handlers
import re
import subprocess
import sys
import types
class ErrorHandler(object):
"""
class_obj = ErrorHandler()
DESCRIPTION:
errorhandler is a class to control, and attempt to fix, exceptions
generated in scripts.
ErrorHandler should NEVER be called or included within a user script.
This class is called ESXCLUSIVELY by the trywrappers.handlertry
decorator.
USAGE:
=========================================================
Currently, these handlers only work within class objects.
=========================================================
The errorhandling system can be used in code in two ways:
1. In this form , the 'TriggerMessage' handles the entire method.
In this case, any error will be handled by the handlertry, and
there is no need for an additional try/except.
<myclass>
@handlertry("TriggerMessage: ", tries = 2)
def myMethod(self, args, kwargs):
FH = open(filename, "w+", 0)
return FH
Some more code here (After handling, control always returns here,
unless a controlled program end is called.)
2. In this form, the @handlertry is left generic, and the TriggerMessage
is passed through the error message. In this case, a try/except
must be included to add the TriggerMessage to the error.
@handlertry("")
def myMethod(self, args, kwargs)::
try:
FH = open(filename, "w+", 0)
return FH
except (Exception, Exception) as e:
e.message = ('TriggerMessage: ' + e.message)
raise type(e)(e.message)
Some more code here (After handling, control always returns here,
unless a controlled program end is called.)
METHODS:
customErr(e, source, frame)
e = The MODIFIED string passed out from the exception. Normal usage
dictates the string be pre-pended with the name of the error
handling method (see above).
source = The 'getmembers(self)' call found in the self.err call above
frame = The 'stack()' call found in the self.err call above
err(e, source, frame)
Simply a smaller pointer to customErr()
None of these methods are intended or recommended to be user callable.
"""
def __init__(self): #, log):
pass
def err(self):
def _err(callobj, args, kwargs, e, source, frame):
return self.customErr(callobj, args, kwargs, e, source, frame)
return _err
def customErr(self, callobj, tries, args, kwargs, e, GETMEMBERS, STACK):
"""
NAME
errorhandler
FILE
errorhandler.py
DESCRIPTION
Used for generating both custom unhandled exceptions, and
for handling exceptions with specific actions. Creates the opportunity
for cutom code to be attached to errors and resolve them in a clean
format
An existing 'self' object must be passed in with an existing self.log
object associated with it!!
Uses the self.log() parameter of an EXISTING instantiated log object
to generate output. A logger has to be instantiated in the calling
class for this method to be used.
EXAMPLE (USAGE)
from inspect import getmembers
from inspect import stack
import errorhandler
customerr = errorhandler.ErrorHandler(self.log)
self.err = customerr.err()
try:
print this wont work
except:
e = "".join(["ErrorIdentifier: ", "Additional information."])
self.lasterr = self.err(e, getmembers(self), stack())
METHODS
ErrorHandler(self, e, source, frame)
Error message handler. Generates logfile output
self = The class object using the error handler. Must contain
a self.log object.
e = The error message passed from the calling object. I.e.
except Exception, e:
source = The inspect.getmembers(self) passed in from the error
call.
frame = The inspect.stack() passed in from the error call.
TEMPLATE(self, e)
Create custom error message and handling code
HIDDEN METHODS
_format_original_error(e)
_log_error(self, message, e)
"""
###############################################################################
# FOR NOW, LEAVE 'e' ALONE
# # Source is inspect.getmembers(self)
# # EXAMPLE SOURCE (a list of tuples):
# #[('MAX_LENGTH', 16384),
# # ('TSTART', 'TWILIOSOCK'),
# # ('__doc__', None),
# # ('__implemented__', <implementedBy twisted.internet.protocol.Protocol>),
# # ('__init__', <bound method Handler.__init__ of <twistedlisten.Handler instance at 0x02E05EB8>>),
# # ('__module__', 'twistedlisten'),
# # ('__providedBy__', <implementedBy twistedlisten.Handler>),
# # ('__provides__', <implementedBy twistedlisten.Handler>),
# # ('_buffer', ''),
# # ('_busyReceiving', False),
# # ('_checkdata', <bound method Handler._checkdata of <twistedlisten.Handler instance at 0x02E05EB8>>),
# # ('_parsedata', <bound method Handler._parsedata of <twistedlisten.Handler instance at 0x02E05EB8>>),
# # ('<some_method>', <bound method <class>.<method> of <someinstance instance at 0x02E05EB8>>),
# #]
# errorin = str(source[6][1])
# errorin = errorin.replace("implementedBy", "")
# errorin = "".join(c for c in errorin if c not in "<>")
# errorin = errorin + "." + str(frame[0][3])
# errorin = errorin + "(line:" + str(frame[0][2]) + "): "
# e = errorin + str(e)
# import handlers
#333
# This calls the proper handler method from 'handlers'
# NOTE: The error keyword MUST MATCH THE METHOD NAME
# NOTE: A string search is used to match the method name to contents
# of the error string passed in.
# THIS MEANS if you have two methods, 'ERR1' AND 'ERR10', 'ERR1'
# will always be the one found...SO IT IS RECOMMENDED YOU NAME
# THE METHODS VERY UNIQUELY AND CAREFULLY.
# I.e. 'StringNotFoundInLogError'
for key in Handlers.__dict__.keys():
if (str(key).lower() in str(e).lower()):
return Handlers.__dict__[key](callobj, tries, args, kwargs, e, GETMEMBERS, STACK)
log.error(''.join(["errorhandler.customErr: ",
"Didn't find a matching handler for ",
str(callobj), ",",
str(args), ",",
str(kwargs), ",",
str(e)
])
)
key = "UnknownException"
# return Handlers.UnknownException(callobj, args, kwargs, e)
return Handlers.__dict__[key](callobj, tries, args, kwargs, e, GETMEMBERS, STACK)
# def ERROR(name, *args, **kwargs):
# log.error()