[racket] Exception decorator. is it possible?
This code works, but it needs a lot more of error checking.
It uses that all the built-in exceptions are transparent and that all
of them inherit from exn, so the first field is the message.
;---
#lang racket
(define (copy-struct/new-first str first)
(define-values (str-struct-info _) (struct-info str))
(define str-maker (struct-type-make-constructor str-struct-info))
(apply str-maker first (cdr (cdr (vector->list (struct->vector str))))))
(define (parse-item item)
(with-handlers ([exn? (lambda (e) (raise (copy-struct/new-first e
(format "At item ~a\n~a" item (exn-message e)))))])
(cons 1 2 3) ;==> cons: arity mismatch;
#;(/ 1 0) ;==> /: division by zero
))
(parse-item "Hello")
;---
Gustavo
On Wed, Sep 17, 2014 at 1:59 PM, Roman Klochkov <kalimehtar at mail.ru> wrote:
> I'm writeing a parser.
> So I want to see in parser exception what happend and when (not only where).
>
> (define (parse-item item)
> (with-handlers ([exn? (lambda (e) (raise (struct-copy exn e [message
> (format "At item ~a\n~a" item (exn-message e))])))])
> ....))
>
> But this way I lose all exception types. Is there a way to change exception
> message, saving its type, except long copy/paste like
> (with-handlers ([exn:fail:contract:arity? (raise (struct-copy
> exn:fail:contract:arity ...))]
> [exn:fil:contract? (raise (struct-copy
> exn:fail:contract ...))]
> ...)
> ...)
>
> ?
>
> --
> Roman Klochkov
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>