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 (if trap-idx
76 (format #f "Trap ~d: ~a" trap-idx trap-name)
78 (debug (make-debug stack 0 error-msg)))
83 (format #t "~a~%" error-msg)
84 (format #t "Entering a new prompt. ")
85 (format #t "Type `,bt' for a backtrace or `,q' to continue.\n")))
86 ((@ (system repl repl) start-repl) #:debug debug)))))
88 (define (null-trap-handler frame trap-idx trap-name)
91 (define le-trap-handler
93 ((debug) debug-trap-handler)
94 ((pass) null-trap-handler)
96 (else (error "Unknown trap-handler strategy" trap-handler))))
100 (with-default-trap-handler le-trap-handler
101 (lambda () (%start-stack #t thunk))))
106 (if (memq key pass-keys)
107 (apply throw key args)
110 ((,subr ,msg ,args . ,rest)
113 (run-hook before-error-hook)
114 (display-error #f err subr msg args rest)
115 (run-hook after-error-hook)
116 (force-output err))))
118 (format err "\nERROR: uncaught throw to `~a', args: ~a\n"
123 (if (memq key pass-keys)
124 (apply throw key args))))
126 (if (procedure? post-error)
127 post-error ; a handler proc
128 (error "Unknown post-error strategy" post-error))))
133 (let* ((tag (and (pair? (fluid-ref %stacks))
134 (cdar (fluid-ref %stacks))))
135 (stack (narrow-stack->vector
137 ;; Cut three frames from the top of the stack:
138 ;; make-stack, this one, and the throw handler.
140 ;; Narrow the end of the stack to the most recent
143 ;; And one more frame, because %start-stack invoking
144 ;; the start-stack thunk has its own frame too.
146 (error-msg (error-string stack key args))
147 (debug (make-debug stack 0 error-msg)))
150 (format #t error-msg)
151 (format #t "Entering a new prompt. ")
152 (format #t "Type `,bt' for a backtrace or `,q' to continue.\n")
153 ((@ (system repl repl) start-repl) #:debug debug))))))
156 ;; fall through to rethrow
159 (if (procedure? on-error)
160 on-error ; pre-unwind handler
161 (error "Unknown on-error strategy" on-error)))))))
163 (define-syntax with-error-handling
166 (call-with-error-handling (lambda () form)))))