Start trying to improve exceptions and backtraces
When using knots utilities.
This commit is contained in:
parent
bddc6c04ad
commit
893299ba24
3 changed files with 125 additions and 40 deletions
72
knots.scm
72
knots.scm
|
@ -1,11 +1,21 @@
|
||||||
(define-module (knots)
|
(define-module (knots)
|
||||||
|
#:use-module (srfi srfi-1)
|
||||||
|
#:use-module (ice-9 match)
|
||||||
#:use-module (ice-9 suspendable-ports)
|
#:use-module (ice-9 suspendable-ports)
|
||||||
#:use-module (fibers conditions)
|
#:use-module (fibers conditions)
|
||||||
|
#:use-module (system repl debug)
|
||||||
#:export (call-with-default-io-waiters
|
#:export (call-with-default-io-waiters
|
||||||
|
|
||||||
wait-when-system-clock-behind
|
wait-when-system-clock-behind
|
||||||
|
|
||||||
call-with-sigint))
|
call-with-sigint
|
||||||
|
|
||||||
|
&knots-exception
|
||||||
|
make-knots-exception
|
||||||
|
knots-exception?
|
||||||
|
knots-exception-stack
|
||||||
|
|
||||||
|
print-backtrace-and-exception/knots))
|
||||||
|
|
||||||
(define (call-with-default-io-waiters thunk)
|
(define (call-with-default-io-waiters thunk)
|
||||||
(parameterize
|
(parameterize
|
||||||
|
@ -37,3 +47,63 @@
|
||||||
(sigaction SIGINT (car handler) (cdr handler))
|
(sigaction SIGINT (car handler) (cdr handler))
|
||||||
;; restore original C handler.
|
;; restore original C handler.
|
||||||
(sigaction SIGINT #f))))))
|
(sigaction SIGINT #f))))))
|
||||||
|
|
||||||
|
(define &knots-exception
|
||||||
|
(make-exception-type '&knots-exception
|
||||||
|
&exception
|
||||||
|
'(stack)))
|
||||||
|
|
||||||
|
(define make-knots-exception
|
||||||
|
(record-constructor &knots-exception))
|
||||||
|
|
||||||
|
(define knots-exception?
|
||||||
|
(record-predicate &knots-exception))
|
||||||
|
|
||||||
|
(define knots-exception-stack
|
||||||
|
(exception-accessor
|
||||||
|
&knots-exception
|
||||||
|
(record-accessor &knots-exception 'stack)))
|
||||||
|
|
||||||
|
(define* (print-backtrace-and-exception/knots
|
||||||
|
exn
|
||||||
|
#:key (port (current-error-port)))
|
||||||
|
(let* ((stack (match (fluid-ref %stacks)
|
||||||
|
((stack-tag . prompt-tag)
|
||||||
|
(make-stack #t
|
||||||
|
0 prompt-tag
|
||||||
|
0 (and prompt-tag 1)))))
|
||||||
|
(error-string
|
||||||
|
(call-with-output-string
|
||||||
|
(lambda (port)
|
||||||
|
(let ((knots-stacks
|
||||||
|
(map knots-exception-stack
|
||||||
|
(filter knots-exception?
|
||||||
|
(simple-exceptions exn)))))
|
||||||
|
|
||||||
|
(let ((stack-vec
|
||||||
|
(stack->vector stack)))
|
||||||
|
(print-frames (list->vector
|
||||||
|
(drop
|
||||||
|
(vector->list stack-vec)
|
||||||
|
6))
|
||||||
|
port
|
||||||
|
#:count (stack-length stack)))
|
||||||
|
(for-each
|
||||||
|
(lambda (stack)
|
||||||
|
(let ((stack-vec
|
||||||
|
(stack->vector stack)))
|
||||||
|
(print-frames (list->vector
|
||||||
|
(drop
|
||||||
|
(vector->list stack-vec)
|
||||||
|
3))
|
||||||
|
port
|
||||||
|
#:count (stack-length stack))))
|
||||||
|
knots-stacks)
|
||||||
|
(print-exception
|
||||||
|
port
|
||||||
|
(if (null? knots-stacks)
|
||||||
|
(stack-ref stack 1)
|
||||||
|
(stack-ref (last knots-stacks) 3))
|
||||||
|
'%exception
|
||||||
|
(list exn)))))))
|
||||||
|
(display error-string port)))
|
||||||
|
|
|
@ -21,9 +21,11 @@
|
||||||
#:use-module (srfi srfi-1)
|
#:use-module (srfi srfi-1)
|
||||||
#:use-module (srfi srfi-71)
|
#:use-module (srfi srfi-71)
|
||||||
#:use-module (ice-9 match)
|
#:use-module (ice-9 match)
|
||||||
|
#:use-module (ice-9 control)
|
||||||
#:use-module (fibers)
|
#:use-module (fibers)
|
||||||
#:use-module (fibers channels)
|
#:use-module (fibers channels)
|
||||||
#:use-module (fibers operations)
|
#:use-module (fibers operations)
|
||||||
|
#:use-module (knots)
|
||||||
#:export (fibers-batch-map
|
#:export (fibers-batch-map
|
||||||
fibers-map
|
fibers-map
|
||||||
|
|
||||||
|
@ -41,19 +43,23 @@
|
||||||
(let ((reply (make-channel)))
|
(let ((reply (make-channel)))
|
||||||
(spawn-fiber
|
(spawn-fiber
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(with-exception-handler
|
(call-with-escape-continuation
|
||||||
(lambda (exn)
|
(lambda (return)
|
||||||
(put-message reply (cons 'exception exn)))
|
(with-exception-handler
|
||||||
(lambda ()
|
(lambda (exn)
|
||||||
(call-with-values
|
(match (fluid-ref %stacks)
|
||||||
(lambda ()
|
((stack-tag . prompt-tag)
|
||||||
(with-throw-handler #t
|
(let ((stack (make-stack #t
|
||||||
thunk
|
0 prompt-tag
|
||||||
(lambda _
|
0 (and prompt-tag 1))))
|
||||||
(backtrace))))
|
(put-message reply (list 'exception exn stack)))))
|
||||||
(lambda vals
|
(return))
|
||||||
(put-message reply vals))))
|
(lambda ()
|
||||||
#:unwind? #t))
|
(call-with-values
|
||||||
|
(lambda ()
|
||||||
|
(start-stack #t (thunk)))
|
||||||
|
(lambda vals
|
||||||
|
(put-message reply vals))))))))
|
||||||
#:parallel? #t)
|
#:parallel? #t)
|
||||||
reply))
|
reply))
|
||||||
|
|
||||||
|
@ -62,8 +68,13 @@
|
||||||
reply-channels)))
|
reply-channels)))
|
||||||
(map
|
(map
|
||||||
(match-lambda
|
(match-lambda
|
||||||
(('exception . exn)
|
(('exception exn stack)
|
||||||
(raise-exception exn))
|
(let ((knots-exn
|
||||||
|
(make-knots-exception stack)))
|
||||||
|
(raise-exception
|
||||||
|
(make-exception
|
||||||
|
knots-exn
|
||||||
|
exn))))
|
||||||
(result
|
(result
|
||||||
(apply values result)))
|
(apply values result)))
|
||||||
responses)))
|
responses)))
|
||||||
|
@ -234,21 +245,22 @@
|
||||||
(get-message channel))))
|
(get-message channel))))
|
||||||
(put-message
|
(put-message
|
||||||
reply-channel
|
reply-channel
|
||||||
(with-exception-handler
|
(call-with-escape-continuation
|
||||||
(lambda (exn)
|
(lambda (return)
|
||||||
(cons 'exception exn))
|
(with-exception-handler
|
||||||
(lambda ()
|
(lambda (exn)
|
||||||
(with-throw-handler #t
|
(match (fluid-ref %stacks)
|
||||||
(lambda ()
|
((stack-tag . prompt-tag)
|
||||||
(call-with-values
|
(let ((stack (make-stack #t
|
||||||
(lambda ()
|
0 prompt-tag
|
||||||
(apply proc args))
|
0 (and prompt-tag 1))))
|
||||||
(lambda vals
|
(return (list 'exception exn stack))))))
|
||||||
(cons 'result vals))))
|
(lambda ()
|
||||||
(lambda args
|
(call-with-values
|
||||||
(when (apply show-backtrace? args)
|
(lambda ()
|
||||||
(backtrace)))))
|
(start-stack #t (apply proc args)))
|
||||||
#:unwind? #t)))))
|
(lambda vals
|
||||||
|
(cons 'result vals)))))))))))
|
||||||
#:parallel? #t))
|
#:parallel? #t))
|
||||||
(iota parallelism))
|
(iota parallelism))
|
||||||
|
|
||||||
|
@ -257,4 +269,10 @@
|
||||||
(put-message channel (cons reply-channel args))
|
(put-message channel (cons reply-channel args))
|
||||||
(match (get-message reply-channel)
|
(match (get-message reply-channel)
|
||||||
(('result . vals) (apply values vals))
|
(('result . vals) (apply values vals))
|
||||||
(('exception . exn) (raise-exception exn)))))))
|
(('exception exn stack)
|
||||||
|
(let ((knots-exn
|
||||||
|
(make-knots-exception stack)))
|
||||||
|
(raise-exception
|
||||||
|
(make-exception
|
||||||
|
knots-exn
|
||||||
|
exn)))))))))
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
#:use-module (web http)
|
#:use-module (web http)
|
||||||
#:use-module (web request)
|
#:use-module (web request)
|
||||||
#:use-module (web response)
|
#:use-module (web response)
|
||||||
|
#:use-module (knots)
|
||||||
#:use-module (knots timeout)
|
#:use-module (knots timeout)
|
||||||
#:use-module (knots non-blocking)
|
#:use-module (knots non-blocking)
|
||||||
#:export (run-knots-web-server
|
#:export (run-knots-web-server
|
||||||
|
@ -203,8 +204,7 @@ on the procedure being called at any particular time."
|
||||||
#f)
|
#f)
|
||||||
|
|
||||||
(define (default-exception-handler exn request)
|
(define (default-exception-handler exn request)
|
||||||
(let* ((stack (make-stack #t))
|
(let* ((error-string
|
||||||
(error-string
|
|
||||||
(call-with-output-string
|
(call-with-output-string
|
||||||
(lambda (port)
|
(lambda (port)
|
||||||
(simple-format
|
(simple-format
|
||||||
|
@ -212,12 +212,9 @@ on the procedure being called at any particular time."
|
||||||
"exception when processing: ~A ~A\n"
|
"exception when processing: ~A ~A\n"
|
||||||
(request-method request)
|
(request-method request)
|
||||||
(uri-path (request-uri request)))
|
(uri-path (request-uri request)))
|
||||||
(display-backtrace stack port 4)
|
(print-backtrace-and-exception/knots
|
||||||
(print-exception
|
exn
|
||||||
port
|
#:port port)))))
|
||||||
(stack-ref stack 4)
|
|
||||||
'%exception
|
|
||||||
(list exn))))))
|
|
||||||
(display error-string
|
(display error-string
|
||||||
(current-error-port)))
|
(current-error-port)))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue