/[meta-cvs]/meta-cvs/F-9AB435A23565E6385CE7F4F347D7A205
ViewVC logotype

Contents of /meta-cvs/F-9AB435A23565E6385CE7F4F347D7A205

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Sun Feb 10 04:09:51 2002 UTC (12 years, 2 months ago) by kaz
Branch: MAIN
Added a much improved error handling routine in anticipation
of better use of restarts.

* error.lisp: New file
(restart-destructure): New macro.
(*mcvs-error-treatment*): Special variable, moved here from
mcvs-main.
(mcvs-error-handler): New function.

* mcvs-main.lisp (mcvs-top-error-handler): Function removed.
(mcvs-execute): Bind error to new mcvs-error-handler instad
of mcvs-top-error-handler.
1 ;;; This source file is part of the Meta-CVS program,
2 ;;; which is distributed under the GNU license.
3 ;;; Copyright 2002 Kaz Kylheku
4
5 (require "chatter")
6 (provide "error")
7
8 (defmacro restart-destructure ((rest-var &rest var-sym-pairs) &body forms)
9 (let ((restarts-sym (gensym "RESTARTS-")))
10 `(let ((,restarts-sym (compute-restarts)))
11 (let (,@(mapcar #'(lambda (var-sym-pair)
12 `(,(first var-sym-pair)
13 (find ',(second var-sym-pair) ,restarts-sym
14 :key #'restart-name)))
15 var-sym-pairs)
16 (,rest-var (remove-if #'(lambda (name)
17 (find name
18 ',(mapcar #'second
19 var-sym-pairs)))
20 ,restarts-sym :key #'restart-name)))
21 ,@forms))))
22
23 (defvar *mcvs-error-treatment* :interactive
24 "This variable is used by the top level error handler set up in mcvs-execute to
25 decide on what to do with a restartable error condition. If no restarts are
26 available, then this variable is ignored; the handler will print the error
27 message and terminate the program. If the error is restartable, then this
28 variable is examined. A value of :interactive indicates that a menu of options
29 should be presented to a user, who can choose to bail the program, or invoke
30 one of the available restarts. A value of :continue means to emit a warning
31 message and then invoke the a continue restart if one is available. If restarts
32 are available, but not ones that can be automatically selected by the handler,
33 then it will terminate the program. A value of :terminate means to terminate
34 on error, restartable or not. A value of :decline means to return normally
35 handling the error.")
36
37 (defun mcvs-error-handler (condition)
38 (let ((*print-escape* nil))
39 (restart-destructure (others (continue continue))
40 (ecase *mcvs-error-treatment*
41 ((:interactive)
42 (when (null (compute-restarts))
43 (print-object condition *error-output*)
44 (throw 'mcvs-terminate t))
45 (let* (command-list
46 (menu (with-output-to-string (stream)
47 (format stream "~%The following error has occured:~%~%")
48 (format stream " ~a~%~%" condition)
49 (format stream "You have these alternatives:~%~%")
50 (format stream " ?) Re-print this menu.~%" continue)
51 (when continue
52 (format stream " C) ~a~%" continue)
53 (format stream " A) Auto-continue all continuable errors.~%")
54 (push (list "C" #'(lambda ()
55 (invoke-restart continue)))
56 command-list)
57 (push (list "A" #'(lambda ()
58 (setf *mcvs-error-treatment*
59 :continue)
60 (invoke-restart continue)))
61 command-list))
62 (when others
63 (let ((counter 0))
64 (dolist (restart others)
65 (format stream " ~a) ~a~%" (incf counter) restart)
66 (push (list (format nil "~a" counter)
67 #'(lambda ()
68 (invoke-restart restart)))
69 command-list))))
70 (format stream " T) Terminate gracefully.~%~%")
71 (push (list "T" #'(lambda ()
72 (throw 'mcvs-terminate t)))
73 command-list))))
74 (write-string menu)
75 (loop
76 (write-string ">")
77 (let* ((line (read-line))
78 (command (find line command-list
79 :key #'first
80 :test #'string-equal)))
81 (cond
82 ((string= line "?")
83 (write-string menu))
84 (command
85 (funcall (second command)))
86 (t (format t "What?~%")))))))
87 ((:continue)
88 (when continue
89 (chatter-terse "Auto-continuing error:~%" condition)
90 (chatter-terse " ~a~%" condition)
91 (invoke-restart continue))
92 (print-object condition *error-output*)
93 (throw 'mcvs-terminate t))
94 ((:terminate)
95 (print-object condition *error-output*)
96 (throw 'mcvs-terminate t))
97 ((:decline)
98 (return-from mcvs-error-handler nil))))))

  ViewVC Help
Powered by ViewVC 1.1.5