1 ;;; Error handling in the REPL
3 ;; Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 (define-module (system repl error-handling)
23 #:use-module (system base pmatch)
24 #:use-module (system vm trap-state)
25 #:use-module (system repl debug)
26 #:export (call-with-error-handling
33 ;;; Error handling via repl debugging
36 (define (error-string stack key args)
37 (with-output-to-string
40 ((,subr ,msg ,args . ,rest)
41 (display-error (vector-ref stack 0) (current-output-port)
44 (format #t "Throw to key `~a' with args `~s'." key args))))))
46 (define* (call-with-error-handling thunk #:key
47 (on-error 'debug) (post-error 'catch)
48 (pass-keys '(quit)) (trap-handler 'debug))
49 (let ((in (current-input-port))
50 (out (current-output-port))
51 (err (current-error-port)))
52 (define (with-saved-ports thunk)
53 (with-input-from-port in
55 (with-output-to-port out
57 (with-error-to-port err
60 (define (debug-trap-handler frame trap-idx trap-name)
61 (let* ((tag (and (pair? (fluid-ref %stacks))
62 (cdar (fluid-ref %stacks))))
63 (stack (narrow-stack->vector
65 ;; Take the stack from the given frame, cutting 0
68 ;; Narrow the end of the stack to the most recent
71 ;; And one more frame, because %start-stack
72 ;; invoking the start-stack thunk has its own frame
75 (error-msg (format #f "Trap ~d: ~a" trap-idx trap-name))
76 (debug (make-debug stack 0 error-msg)))
79 (format #t "~a~%" error-msg)
80 (format #t "Entering a new prompt. ")
81 (format #t "Type `,bt' for a backtrace or `,q' to continue.\n")
82 ((@ (system repl repl) start-repl) #:debug debug)))))
84 (define (null-trap-handler frame trap-idx trap-name)
87 (define le-trap-handler
89 ((debug) debug-trap-handler)
90 ((pass) null-trap-handler)
92 (else (error "Unknown trap-handler strategy" trap-handler))))
96 (with-default-trap-handler le-trap-handler
97 (lambda () (%start-stack #t thunk))))
102 (if (memq key pass-keys)
103 (apply throw key args)
106 ((,subr ,msg ,args . ,rest)
109 (run-hook before-error-hook)
110 (display-error #f err subr msg args rest)
111 (run-hook after-error-hook)
112 (force-output err))))
114 (format err "\nERROR: uncaught throw to `~a', args: ~a\n"
119 (if (memq key pass-keys)
120 (apply throw key args))))
122 (if (procedure? post-error)
123 post-error ; a handler proc
124 (error "Unknown post-error strategy" post-error))))
129 (let* ((tag (and (pair? (fluid-ref %stacks))
130 (cdar (fluid-ref %stacks))))
131 (stack (narrow-stack->vector
133 ;; Cut three frames from the top of the stack:
134 ;; make-stack, this one, and the throw handler.
136 ;; Narrow the end of the stack to the most recent
139 ;; And one more frame, because %start-stack invoking
140 ;; the start-stack thunk has its own frame too.
142 (error-msg (error-string stack key args))
143 (debug (make-debug stack 0 error-msg)))
146 (format #t error-msg)
147 (format #t "Entering a new prompt. ")
148 (format #t "Type `,bt' for a backtrace or `,q' to continue.\n")
149 ((@ (system repl repl) start-repl) #:debug debug))))))
152 ;; fall through to rethrow
155 (if (procedure? on-error)
156 on-error ; pre-unwind handler
157 (error "Unknown on-error strategy" on-error)))))))
159 (define-syntax with-error-handling
162 (call-with-error-handling (lambda () form)))))