printing errors

64 views
Skip to first unread message

Shriram Krishnamurthi

unread,
Aug 27, 2020, 4:31:19 PM8/27/20
to Racket Users
Given an exception, is there a way to print the error using Racket's conventional error printing machinery (e.g., in color in DrRacket, etc.), without halting execution?

I would like to be able to integrate this with #%printing-module-begin and #%top-interaction. Unfortunately, those by default will halt execution after printing the message.

Robby Findler

unread,
Aug 27, 2020, 4:35:23 PM8/27/20
to Shriram Krishnamurthi, Racket Users
Yes, something like this:

(define (print-exn exn)
    ((error-display-handler)
     (if (exn? exn)
         (exn-message exn)
         (format "~a" exn))
     exn))

On Thu, Aug 27, 2020 at 3:31 PM Shriram Krishnamurthi <shr...@gmail.com> wrote:
Given an exception, is there a way to print the error using Racket's conventional error printing machinery (e.g., in color in DrRacket, etc.), without halting execution?

I would like to be able to integrate this with #%printing-module-begin and #%top-interaction. Unfortunately, those by default will halt execution after printing the message.

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/a6277017-9dc8-44d1-8c97-119cb431cf4bn%40googlegroups.com.

Shriram Krishnamurthi

unread,
Aug 27, 2020, 6:11:00 PM8/27/20
to Robby Findler, Racket Users
This is perfect, thanks!

Two follow-up questions:

1. The error printer seems to print an extra newline at the end relative to what the port-display-handler (for instance) shows. Is there a way to suppress that?

2. The stack trace seems to be extracted automatically. Is there a way to suppress it entirely?

Shriram 

Sorawee Porncharoenwase

unread,
Aug 27, 2020, 6:18:02 PM8/27/20
to Shriram Krishnamurthi, Robby Findler, Racket Users

For stacktrace, use error-print-context-length to suppress it:

(define (print-exn exn)
  (parameterize ([error-print-context-length 0])
    ((error-display-handler)
     (if (exn? exn)
         (exn-message exn)
         (format "~a" exn))
     exn)))

I don’t see any extra newline though.


--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.

Robby Findler

unread,
Aug 27, 2020, 7:02:38 PM8/27/20
to Sorawee Porncharoenwase, Shriram Krishnamurthi, Racket Users
It may be that you're seeing the "clever"ness of the DrRacket repl. If the last character printed is a newline, it only shows it when there is another character after the newline. 

Robby

Shriram Krishnamurthi

unread,
Aug 29, 2020, 11:11:56 AM8/29/20
to Sorawee Porncharoenwase, Robby Findler, Racket Users
Thank you!

Is there a way of further suppressing info? Right now I get output like

image.png ../../make-semantics.rkt:37:13: a: undefined

which is a reference to the language implementation file rather than to the program in the language.

The programs here are so small that suppressing everything but the "a: undefined" would be great.

Robby Findler

unread,
Aug 29, 2020, 11:17:37 AM8/29/20
to Shriram Krishnamurthi, Sorawee Porncharoenwase, Racket Users
It might make the most sense for you to extract the exn-message and print it yourself? It should contain only the "a: undefined" part. If you want to selectively access the stacktrace and decide whether or not to print that first stackframe, that'll probably require a change to DrRacket, tho -- at least if you have debugging turned on. (There may be some clever hack I'm not thinking of, tho, depending on what you want.)

Robby

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.

Sorawee Porncharoenwase

unread,
Aug 29, 2020, 3:58:20 PM8/29/20
to Robby Findler, Shriram Krishnamurthi, Racket Users

Alternatively, I think you can simply supply a non-exception value to the handler. This will suppress the red cross icon too, though.

(define (print-exn exn)
  ((error-display-handler)
   (if (exn? exn)
       (exn-message exn)
       (format "~a" exn))
   #f))
Reply all
Reply to author
Forward
0 new messages