[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: reporting 'system-error informatively
From: |
Neil Jerram |
Subject: |
Re: reporting 'system-error informatively |
Date: |
21 Oct 2002 19:55:57 +0100 |
User-agent: |
Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 |
>>>>> "Marius" == Marius Vollmer <address@hidden> writes:
>> Is there any way to get the arguments themselves, as plain Scheme
>> values instead of text?
Marius> Probably. Messing around with the 'stack' data structure is
probably
Marius> the right thing. Sorry, I can't say more. (But others might.)
Assuming that `the-last-stack' has caught the error that you want to
look at, you can get the stack object by
(define s (fluid-ref the-last-stack))
and then the innermost stack frame by
(define f (stack-ref s 0))
Now, a frame can be either an application or an evaluation, and you'll
often find that the innermost frame is an application, with the one
just higher being an evaluation, e.g.
innermost: [string-length 4]
1 outer: (string-length 4)
(frame-procedure? f) tells you whether the frame is an application.
If it is, (frame-procedure f) returns the procedure and
(frame-arguments f) returns the already evaluated args. If it isn't,
(frame-source f) -- i.e. the frame is an evaluation -- returns the
expression that was being evaluated, which is all you can get.
So (untested as usual) ...
(define (last-error->proc+args)
(let* ((stack (fluid-ref the-last-stack))
(stacklen (stack-length stack)))
(let loop ((index 0))
(if (< index stacklen)
(let ((frame (stack-ref stack index)))
(if (frame-procedure? frame)
(values (frame-procedure frame)
(frame-arguments frame))
(loop (+ index 1))))
#f))))
If `the-last-stack' hasn't captured the error that you want, you can
capture it for yourself using `lazy-catch' and `make-stack':
(define my-stack #f)
(define (saving-error-to-my-stack proc)
(define (lazy-handler key . args)
(set! my-stack (make-stack #t lazy-handler))
(apply throw key args))
(lazy-catch #t
proc
lazy-handler))
Notes - (i) the `lazy-handler' in the `make-stack' call tells
make-stack to return a stack object whose innermost frame is just
outside the call into lazy-handler; (ii) the `#t' in the make-stack
call means "here"; (iii) you must always rethrow from a lazy handler,
hence the `(apply throw ...)' line.
This should of course be in the manual. If anyone feels like working
this into an appropriate patch, I'd appreciate it.
Neil