[plt-scheme] Getting source positions in MzScheme error messages

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Thu Jan 27 17:28:14 EST 2005

Jim Blandy wrote:

> How can I get a source location along with my error message in an
> MzScheme script?
>     $ cat bug.scm
>     (car '())
>     $ mzscheme -r bug.scm
>     bash: mzscheme: command not found
>     $ plt mzscheme -r bug.scm
>     car: expects argument of type <pair>; given ()
>     $
> I'm sure this is a FAQ, but the MzScheme folks seem to quite admirably
> forgo having a FAQ document in favor of having organized and current
> manuals.  I'm happy to read, but I've not found anything.

The easy way is to run it in DrScheme.

But using the errortrace collection is also easy:

   $ /c/Programmer/PLT/MzScheme.exe -M errortrace -r bug.scm
   car: expects argument of type <pair>; given ()
   c:\bug.scm:2:0: (car (quote ()))

How does errortrace work?

Searching for "error" in the HelpDesk reveals:

   (error-display-handler [proc]) gets or sets a procedure that takes
   two arguments: a string to print as an error message, and a value
   representing a raised exception. This error display handler is called
   by the default exception handler with an error message and the exception
   value. The default error display handler displays its first argument to
   the current error port (determined by the current-error-port parameter)
   and ignores the second argument. [10] To report a run-time error, use
   raise (see section 6.1) or error (see section 6.2) instead of calling
   the error display procedure directly. If an exception is raised while
   the error display handler is executing, an error message is printed
   using a primitive error printer and the primitive error escape handler
   is invoked.

The footnote [10] says:

   The default error display handler in DrScheme uses the second argument
   to highlight a source location.

The errortace collection thus replaces the normal error-display-handler
with one that extracts syntax-location from the continuation marks of
the other value.

Jens Axel Søgaard

Posted on the users mailing list.