better error messages when using list-of

capture error and append to message
main
Matthew Butterick 2 years ago
parent eb40e8b576
commit e9955c9d25

@ -1,5 +1,6 @@
#lang debug racket/base #lang debug racket/base
(require racket/match (require racket/match
racket/string
(for-syntax racket/base) (for-syntax racket/base)
"param.rkt" "param.rkt"
"quad.rkt") "quad.rkt")
@ -24,12 +25,10 @@
(define (make-pipeline . passes) (define (make-pipeline . passes)
(pipeline passes)) (pipeline passes))
(define (print-pass . passes)
(apply make-pipeline (append passes (list pass-printer))))
(define (compiler-append c passes) (define (compiler-append c passes)
(make-pipeline (append (pipeline-passes c) passes))) (make-pipeline (append (pipeline-passes c) passes)))
(define-syntax (define-pass stx) (define-syntax (define-pass stx)
(syntax-case stx () (syntax-case stx ()
[(_ (PASS-NAME ARG OTHER-ARG ...) [(_ (PASS-NAME ARG OTHER-ARG ...)
@ -41,9 +40,15 @@
(procedure-rename (procedure-rename
#,(syntax/loc stx #,(syntax/loc stx
(λ (ARG) (λ (ARG)
(define ((make-failure-handler failure-msg) exn)
(raise (make-exn:fail:contract
(string-replace (exn-message exn) "contract violation" (string-append "contract violation in " failure-msg)) (exn-continuation-marks exn))))
(when (current-use-preconditions?) (when (current-use-preconditions?)
(unless (PRECOND-PROC ARG) (define failure-msg (format "~a pass (as precondition)" 'PASS-NAME))
(raise-argument-error 'PASS-NAME (format "~a (as precondition)" 'PRECOND-PROC) ARG))) (with-handlers ([exn:fail:contract? (make-failure-handler failure-msg)])
(unless (PRECOND-PROC ARG)
(raise-argument-error 'PASS-NAME (symbol->string 'PRECOND-PROC) ARG))))
;; a pass can be functional or mutational. ;; a pass can be functional or mutational.
;; if it returns void, assume mutational ;; if it returns void, assume mutational
;; and return the input item. ;; and return the input item.
@ -53,11 +58,13 @@
(begin0 (begin0
res res
(when (current-use-postconditions?) (when (current-use-postconditions?)
(unless (POSTCOND-PROC res) (define failure-msg (format "~a pass (as postcondition)" 'PASS-NAME))
(raise-argument-error 'PASS-NAME (format "~a (as postcondition)" 'POSTCOND-PROC) res)))))) (with-handlers ([exn:fail:contract? (make-failure-handler failure-msg)])
(unless (POSTCOND-PROC res)
(raise-argument-error 'PASS-NAME (symbol->string 'POSTCOND-PROC) ARG)))))))
'PASS-NAME)))])) 'PASS-NAME)))]))
(define-pass (pass-printer qs) (define-pass (print-pass qs)
#:pre values #:pre values
#:post values #:post values
(for-each println qs)) (for-each println qs))
Loading…
Cancel
Save