author | Claus Gittinger <cg@exept.de> |
Thu, 02 Sep 1999 23:44:11 +0200 | |
changeset 4670 | 21eec331e1e9 |
parent 4586 | 22c84f7f9b85 |
child 4937 | 7e723f1daca7 |
permissions | -rw-r--r-- |
4554 | 1 |
" |
2 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
||
14 |
||
4528 | 15 |
GenericException subclass:#NoHandlerError |
16 |
instanceVariableNames:'' |
|
17 |
classVariableNames:'EmergencyHandler' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Kernel-Exceptions' |
|
20 |
! |
|
21 |
||
22 |
!NoHandlerError class methodsFor:'documentation'! |
|
23 |
||
4554 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
||
37 |
||
38 |
! |
|
39 |
||
4528 | 40 |
documentation |
41 |
" |
|
42 |
NoHandlerError is raised, if there is no exception handler |
|
43 |
or default action for an exception. |
|
44 |
||
45 |
The parameter is the unhandled exception. |
|
4554 | 46 |
|
47 |
[Class variables:] |
|
48 |
EmergencyHandler <Block> this block is evaluated, if no handler was defined |
|
49 |
for a signal (i.e. this one is responsible for the |
|
50 |
unhandled exception debugger). |
|
51 |
Having this being a block allows to globally catch |
|
52 |
these errors - even when no enclosing handler-scope |
|
53 |
around the erronous code exists. |
|
54 |
(as the catch/through does). |
|
55 |
||
56 |
||
57 |
[see also:] |
|
58 |
GenericException |
|
59 |
(``Exception handling and signals'': programming/exceptions.html) |
|
4528 | 60 |
" |
61 |
||
62 |
||
4554 | 63 |
! |
64 |
||
65 |
examples |
|
66 |
" |
|
67 |
The emergencyHandler stuff is very useful, to prevent endUser applications |
|
68 |
from entering the debugger. |
|
69 |
||
70 |
Some commonly used (useful) emergency handlers are provided in the |
|
71 |
'useful handlers' section; try them to learn more |
|
72 |
(especially, the mailingHandler is fun). |
|
73 |
||
74 |
Of course, these are only examples - you may define your own handler |
|
75 |
block and pass it to the #emergencyHandler: method. |
|
76 |
||
77 |
BTW: the Launchers 'source & debugger' settings menu allows you |
|
78 |
to install either a default or the notifying handler. |
|
79 |
||
80 |
||
81 |
A handler which shows a box, then aborts - (no more debuggers): |
|
82 |
[exBegin] |
|
83 |
NoHandlerError emergencyHandler:(NoHandlerError abortingEmergencyHandler) |
|
84 |
[exEnd] |
|
85 |
||
86 |
||
87 |
A handler which aborts - (no box, no debugger): |
|
88 |
[exBegin] |
|
89 |
NoHandlerError emergencyHandler:[:ex | AbortSignal raise] |
|
90 |
[exEnd] |
|
91 |
||
92 |
||
93 |
try some exception (for demonstration, in some other process): |
|
94 |
[exBegin] |
|
95 |
[ |
|
96 |
#(1 2 3) at:4 |
|
97 |
] fork. |
|
98 |
[exEnd] |
|
99 |
||
100 |
cleanup (switch back to the regular handler, which enters the debugger): |
|
101 |
[exBegin] |
|
102 |
NoHandlerError emergencyHandler:nil |
|
103 |
[exEnd] |
|
104 |
||
105 |
||
106 |
A handler which shows a warnBox and asks for debugging: |
|
107 |
[exBegin] |
|
108 |
NoHandlerError emergencyHandler:(NoHandlerError notifyingEmergencyHandler) |
|
109 |
[exEnd] |
|
110 |
||
111 |
||
112 |
||
113 |
A handler which dumps information to a file (watch the file 'errorTrace.stx'): |
|
114 |
[exBegin] |
|
115 |
NoHandlerError emergencyHandler:(NoHandlerError dumpingEmergencyHandler) |
|
116 |
[exEnd] |
|
117 |
||
118 |
||
119 |
||
120 |
A handler which sends you mail: |
|
121 |
[exBegin] |
|
122 |
NoHandlerError emergencyHandler:(NoHandlerError mailingEmergencyHandler) |
|
123 |
[exEnd] |
|
124 |
" |
|
125 |
||
4528 | 126 |
! ! |
127 |
||
128 |
!NoHandlerError class methodsFor:'initialization'! |
|
129 |
||
130 |
initialize |
|
131 |
||
132 |
NotifierString := 'unhandled exception'. |
|
133 |
||
134 |
" |
|
135 |
self initialize |
|
136 |
" |
|
137 |
||
138 |
||
139 |
! ! |
|
140 |
||
141 |
!NoHandlerError class methodsFor:'emergency handlers'! |
|
142 |
||
143 |
emergencyHandler |
|
144 |
"return the handler used for unhandled exceptions. |
|
145 |
||
146 |
If no EmergencyHandler has been set, a handler which enters the |
|
147 |
debugger is returned. |
|
148 |
The debugger is opened by asking the signal for a debug action, |
|
149 |
this allows to provide other debuggers in specialized (subclass-instances) |
|
150 |
of Signal (if that is ever needed)" |
|
151 |
||
152 |
" |
|
153 |
set it up, when called the first time |
|
154 |
" |
|
155 |
EmergencyHandler isNil ifTrue:[ |
|
156 |
EmergencyHandler := [:ex | |
|
157 |
" |
|
158 |
sending it to the signal allows per-signal specific |
|
159 |
debuggers to be implemented in the future |
|
160 |
(for example, segv in primitive code could show things |
|
161 |
on the C-level ..) |
|
162 |
" |
|
4586
22c84f7f9b85
return nil from raise if proceeded in the debugger
Claus Gittinger <cg@exept.de>
parents:
4561
diff
changeset
|
163 |
(ex signal) openDebuggerOnException:ex. |
22c84f7f9b85
return nil from raise if proceeded in the debugger
Claus Gittinger <cg@exept.de>
parents:
4561
diff
changeset
|
164 |
"/ if we arrive here, the debugger proceeded. |
22c84f7f9b85
return nil from raise if proceeded in the debugger
Claus Gittinger <cg@exept.de>
parents:
4561
diff
changeset
|
165 |
"/ the value below is the exceptions value ... |
22c84f7f9b85
return nil from raise if proceeded in the debugger
Claus Gittinger <cg@exept.de>
parents:
4561
diff
changeset
|
166 |
nil |
4528 | 167 |
] |
168 |
]. |
|
169 |
||
170 |
^ EmergencyHandler |
|
171 |
||
172 |
||
173 |
! |
|
174 |
||
175 |
emergencyHandler:aOneArgBlock |
|
176 |
"set the handler used for unhandled exceptions. |
|
177 |
The default (a nil-handler) leads to a debugger to be shown." |
|
178 |
||
179 |
EmergencyHandler := aOneArgBlock |
|
180 |
||
181 |
"ST-80 behavior of first showing a notifier: |
|
182 |
(I prefer to get right into the debugger, though) |
|
183 |
||
184 |
Exception |
|
185 |
emergencyHandler: |
|
186 |
[:ex | self errorNotify:ex errorString ] |
|
187 |
" |
|
188 |
||
189 |
"ST-X behavior of going right into the debugger: |
|
190 |
||
191 |
Exception |
|
192 |
emergencyHandler:nil |
|
193 |
" |
|
194 |
||
195 |
"automatically aborting current operation, on error: |
|
196 |
(may be useful for end-user apps; make certain, |
|
197 |
you have abortSignal handlers at appropriate places) |
|
198 |
||
199 |
Exception |
|
200 |
emergencyHandler: |
|
201 |
[:ex | Object abortSignal raise. ex return. ] |
|
202 |
" |
|
203 |
||
204 |
"finally, traditional language system behavior; dump core ;-) |
|
205 |
||
206 |
Exception |
|
207 |
emergencyHandler: |
|
208 |
[:ex | Smalltalk exitWithCoreDump. ] |
|
209 |
" |
|
210 |
||
211 |
"Modified: 15.1.1997 / 20:49:06 / cg" |
|
212 |
||
213 |
! ! |
|
214 |
||
215 |
!NoHandlerError class methodsFor:'useful handlers'! |
|
216 |
||
217 |
abortingEmergencyHandler |
|
218 |
"return a block (usable as an emergency handler), |
|
219 |
which aborts after showing a warnBox. |
|
220 |
This is useful for endUser applications" |
|
221 |
||
222 |
^ [:ex | self warn:'Error: ' , ex errorString. |
|
223 |
AbortSignal raise |
|
224 |
] |
|
225 |
||
226 |
"test with (try a few halts or CTRL-C's): |
|
227 |
NoHandlerError emergencyHandler:(NoHandlerError abortingEmergencyHandler) |
|
228 |
" |
|
229 |
||
230 |
"back with: |
|
231 |
NoHandlerError emergencyHandler:(NoHandlerError notifyingEmergencyHandler) |
|
232 |
NoHandlerError emergencyHandler:nil |
|
233 |
" |
|
234 |
||
235 |
"Created: 15.1.1997 / 20:13:06 / cg" |
|
236 |
"Modified: 15.1.1997 / 20:15:02 / cg" |
|
237 |
! |
|
238 |
||
239 |
dumpingEmergencyHandler |
|
240 |
"return a block (usable as an emergency handler), |
|
241 |
which dumps the stackBacktrace to a trace file and |
|
242 |
aborts after showing a warnBox. |
|
243 |
This is useful, for endUser application, which are still being |
|
244 |
debugged (i.e. the programmers may have a look at the traceFile |
|
245 |
from time to time). |
|
246 |
||
247 |
Notice: |
|
248 |
The code below is just an example; you may want to change the |
|
249 |
name of the error-file in your application |
|
250 |
(but please: copy the code; do not modify here)" |
|
251 |
||
252 |
^ [:ex | |
|
253 |
|str printedException| |
|
254 |
||
255 |
ex signal == NoHandlerError ifTrue:[ |
|
256 |
printedException := ex parameter. |
|
257 |
] ifFalse:[ |
|
258 |
printedException := ex |
|
259 |
]. |
|
260 |
||
261 |
"/ user interruption is handled specially: |
|
262 |
"/ allow user to choose between proceeding or aborting |
|
263 |
"/ but never dump that information to the file. |
|
264 |
||
265 |
printedException signal == Object userInterruptSignal ifTrue:[ |
|
266 |
(self confirm:'abort current action ?') ifTrue:[ |
|
267 |
AbortSignal raise |
|
268 |
]. |
|
269 |
ex proceedWith:nil |
|
270 |
]. |
|
271 |
||
272 |
"/ |
|
273 |
"/ dump it to 'errorTrace.stx' |
|
274 |
"/ |
|
275 |
str := 'errorTrace.stx' asFilename appendingWriteStream. |
|
276 |
||
277 |
str nextPutLine:('******************************* ' |
|
278 |
, AbsoluteTime now printString |
|
279 |
, ' *******************************'). |
|
280 |
str cr. |
|
281 |
||
282 |
str nextPutLine:('** Error: ' , printedException errorString). |
|
283 |
str nextPutLine:('** Signal: ' , printedException signal printString). |
|
284 |
str nextPutLine:('** Parameter: ' , printedException parameter printString). |
|
285 |
str nextPutLine:('** Process: ' , Processor activeProcess printString). |
|
286 |
str nextPutLine:('** Backtrace:'). |
|
287 |
str cr. |
|
288 |
||
289 |
printedException suspendedContext fullPrintAllOn:str. |
|
290 |
str cr. |
|
291 |
str cr. |
|
292 |
str close. |
|
293 |
||
294 |
"/ send a line to stdErr |
|
295 |
||
296 |
('[warning]: ignored error: ' , printedException errorString) errorPrintCR. |
|
297 |
('[warning]: error information appended to ''errorTrace.stx''') errorPrintCR. |
|
298 |
||
299 |
AbortSignal raise |
|
300 |
] |
|
301 |
||
302 |
"test with (try a few halts or CTRL-C's): |
|
303 |
NoHandlerError emergencyHandler:(NoHandlerError dumpingEmergencyHandler) |
|
304 |
" |
|
305 |
||
306 |
"back with: |
|
307 |
NoHandlerError emergencyHandler:(NoHandlerError notifyingEmergencyHandler) |
|
308 |
NoHandlerError emergencyHandler:nil |
|
309 |
" |
|
310 |
||
311 |
"Created: / 15.1.1997 / 20:14:52 / cg" |
|
312 |
"Modified: / 24.1.1997 / 20:36:21 / cg" |
|
313 |
"Modified: / 4.8.1999 / 08:11:20 / stefan" |
|
314 |
! |
|
315 |
||
316 |
mailingEmergencyHandler |
|
317 |
"return a block (usable as an emergency handler), |
|
318 |
which shows a warnBox and optionally mails a stackBacktrace to a maintainer. |
|
319 |
This is useful, for endUser application, which are still being |
|
320 |
debugged (i.e. the programmers may have a look at the errors). |
|
321 |
||
322 |
Notice: the stuff here is a demonstration only; it should be modified |
|
323 |
for your particular environment ... |
|
324 |
... but please: copy the code and modify there; |
|
325 |
leave the stuff below as it is." |
|
326 |
||
327 |
^ [:ex | |
|
328 |
|str printedException doMail emergencyMailReceiver pipe| |
|
329 |
||
330 |
ex signal == NoHandlerError ifTrue:[ |
|
331 |
printedException := ex parameter. |
|
332 |
] ifFalse:[ |
|
333 |
printedException := ex |
|
334 |
]. |
|
335 |
||
336 |
"/ user interruption is handled specially: |
|
337 |
"/ allow user to choose between proceeding or aborting |
|
338 |
"/ but never dump that information to the file. |
|
339 |
||
340 |
printedException signal == Object userInterruptSignal ifTrue:[ |
|
341 |
(self confirm:'abort current action ?') ifTrue:[ |
|
342 |
AbortSignal raise |
|
343 |
]. |
|
344 |
ex proceedWith:nil |
|
345 |
]. |
|
346 |
||
347 |
"/ somehow get the name of the guy to receive the mail |
|
348 |
"/ you have to implement that yourself. |
|
349 |
||
350 |
"/ emergencyMailReceiver := OneOfYourClass getEmergencyMailReceiver. |
|
351 |
emergencyMailReceiver := OperatingSystem getLoginName. |
|
352 |
||
353 |
emergencyMailReceiver isNil ifTrue:[ |
|
354 |
self warn:(printedException errorString |
|
355 |
, '\\No mailing to service people possible.') withCRs. |
|
356 |
doMail := false. |
|
357 |
] ifFalse:[ |
|
358 |
doMail := self confirm:(printedException errorString |
|
359 |
, '\\Mail error information to the service people (' |
|
360 |
, emergencyMailReceiver , ') ?') withCRs |
|
361 |
]. |
|
362 |
doMail ifTrue:[ |
|
363 |
str := '' writeStream. |
|
364 |
||
365 |
str nextPutLine:('Error notification from ' |
|
366 |
, OperatingSystem getLoginName |
|
367 |
, '@' |
|
368 |
, OperatingSystem getHostName). |
|
369 |
str cr. |
|
370 |
||
371 |
str nextPutLine:('Time: ' , AbsoluteTime now printString). |
|
372 |
str nextPutLine:('Error: ', printedException errorString). |
|
373 |
str nextPutLine:('Signal: ', printedException signal printString). |
|
374 |
str nextPutLine:('Parameter: ', printedException parameter printString). |
|
375 |
str nextPutLine:('Process: ', Processor activeProcess printString). |
|
376 |
str nextPutLine:'Backtrace:'. |
|
377 |
str cr. |
|
378 |
||
379 |
printedException suspendedContext fullPrintAllOn:str. |
|
380 |
str cr;cr. |
|
381 |
||
382 |
str close. |
|
383 |
||
384 |
pipe := PipeStream |
|
385 |
writingTo:'mail ', emergencyMailReceiver. |
|
386 |
pipe notNil ifTrue:[ |
|
387 |
pipe nextPutLine:'Subject: automatic error report'. |
|
388 |
pipe nextPutAll:str contents. |
|
389 |
pipe cr. |
|
390 |
pipe close. |
|
391 |
] |
|
392 |
]. |
|
393 |
||
394 |
AbortSignal raise |
|
395 |
] |
|
396 |
||
397 |
"test with (try a few halts or CTRL-C's): |
|
398 |
NoHandlerError emergencyHandler:(Exception mailingEmergencyHandler) |
|
399 |
" |
|
400 |
||
401 |
"back with: |
|
402 |
NoHandlerError emergencyHandler:(Exception notifyingEmergencyHandler) |
|
403 |
NoHandlerError emergencyHandler:nil |
|
404 |
" |
|
405 |
||
406 |
"Created: / 15.1.1997 / 20:14:52 / cg" |
|
407 |
"Modified: / 15.1.1997 / 21:10:28 / cg" |
|
408 |
"Modified: / 4.8.1999 / 08:11:26 / stefan" |
|
409 |
! |
|
410 |
||
411 |
notifyingEmergencyHandler |
|
412 |
"return a block (usable as an emergency handler for exceptions), |
|
413 |
which does errorNotification before going into the debugger." |
|
414 |
||
415 |
^ [:ex | nil errorNotify:ex errorString from:ex suspendedContext ] |
|
416 |
||
417 |
"test with (try a few halts or CTRL-C's): |
|
418 |
Exception emergencyHandler:(Exception notifyingEmergencyHandler) |
|
419 |
" |
|
420 |
||
421 |
"back with: |
|
422 |
NoHandlerError emergencyHandler:nil |
|
423 |
" |
|
424 |
||
425 |
"Modified: 15.1.1997 / 20:15:12 / cg" |
|
426 |
! ! |
|
427 |
||
428 |
!NoHandlerError methodsFor:'default actions'! |
|
429 |
||
430 |
defaultAction |
|
431 |
"This action is performed, if nobody handles the NoHandlerError. |
|
432 |
Look for either a per-process emergencyHandlerBlock |
|
433 |
or the global emergencyHandler (from Exception) ..." |
|
434 |
||
435 |
|block| |
|
436 |
||
437 |
Processor notNil ifTrue:[ |
|
438 |
"care for signal during startup (Processor not yet created)" |
|
439 |
block := Processor activeProcess emergencySignalHandler. |
|
440 |
]. |
|
441 |
block isNil ifTrue:[ |
|
442 |
block := self class emergencyHandler. |
|
443 |
block isNil ifTrue:[ |
|
444 |
"care for error during startup (Exception not yet initialized)" |
|
4539
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
445 |
^ MiniDebugger |
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
446 |
enterWithMessage:self errorString |
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
447 |
mayProceed:self mayProceed |
4528 | 448 |
]. |
449 |
]. |
|
450 |
||
4586
22c84f7f9b85
return nil from raise if proceeded in the debugger
Claus Gittinger <cg@exept.de>
parents:
4561
diff
changeset
|
451 |
^ block value:self |
4528 | 452 |
|
4561 | 453 |
! |
454 |
||
455 |
noHandler |
|
456 |
"redefined to avoid recursive invocations" |
|
457 |
||
458 |
^ self |
|
4528 | 459 |
! ! |
460 |
||
4539
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
461 |
!NoHandlerError methodsFor:'queries'! |
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
462 |
|
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
463 |
mayProceed |
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
464 |
"return true, if the exception handler is allowed to proceed |
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
465 |
the execution where the exception occured." |
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
466 |
|
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
467 |
parameter isNil ifTrue:[^ true]. |
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
468 |
^ parameter mayProceed |
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
469 |
|
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
470 |
|
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
471 |
! ! |
7064506e8285
debugger entry protocol changed;
Claus Gittinger <cg@exept.de>
parents:
4528
diff
changeset
|
472 |
|
4528 | 473 |
!NoHandlerError class methodsFor:'documentation'! |
474 |
||
475 |
version |
|
4586
22c84f7f9b85
return nil from raise if proceeded in the debugger
Claus Gittinger <cg@exept.de>
parents:
4561
diff
changeset
|
476 |
^ '$Header: /cvs/stx/stx/libbasic/NoHandlerError.st,v 1.5 1999-08-10 18:41:49 cg Exp $' |
4528 | 477 |
! ! |
478 |
NoHandlerError initialize! |