Subject: Re: Is there a portable way to print simple errors readably?
From: rpw3@rpw3.org (Rob Warnock)
Date: Mon, 18 May 2009 21:13:46 -0500
Newsgroups: comp.lang.lisp
Message-ID: <os-dnVlO7dhHiI_XnZ2dnUVZ_vGdnZ2d@speakeasy.net>
Ron Garret  <rNOSPAMon@flownet.com> wrote:
+---------------
| Subject line pretty much says it all.  By "readably" I do not mean so 
| that it can be read by the Lisp reader, I mean so that it can be read by 
| a human.  i.e. if I have a simple-error object generated by:
| 
| (nth-value 1 (ignore-errors (error "foo ~A" 1)))
| 
| is there a portable way to display this object so that it shows up
| as "foo 1" rather than e.g. "#<SIMPLE-ERROR #x300041D55A1D>"
+---------------

Sure, just use PRINC (or FORMAT "~A") to print it instead of
the default WRITE (or FORMAT "~S") that your REPL is using:

    > (format t "~a" (nth-value 1 (ignore-errors (error "foo ~A" 1))))

    Error in function "Top-Level Form":  foo 1
    NIL
    > 


-Rob

p.s. By the way, when writing simple CMUCL-based apps ("shell scripts")
for non-programmers to run, I often install the following in
*DEBUGGER-HOOK* so the user only sees the plaintext error and
then aborts [either back to some command-line processor or punts
out altogether] rather than falling into the CL debugger. This
results in nicer messages to the user without totally obscuring
the tracks if I need to debug something later:

    > (defun simple-debugger-stub (c hook)
	(declare (ignore hook))
	(when (find-restart 'abort c)
	  (format *error-output*
		  "~&~a~%[Condition of type ~a]~%" c (type-of c))
	  (abort c)))
    > (setf *debugger-hook* #'simple-debugger-stub)

    #<Interpreted Function SIMPLE-DEBUGGER-STUB {489F04E9}>
    > (defun fail () (error "foo ~A" 1))

    FAIL
    > (fail)

    Error in function FAIL:  foo 1
    [Condition of type SIMPLE-ERROR]

    > 

Note: In other than CMUCL you might need to find some other way
to get back to the top-level and/or print the error before looking
for the ABORT restart. [ISTR still getting stuck in the debugger
even with the above in place in at least one older version of CLISP...]

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607