Subject: bug#41956: [PATCH] ice-9: exceptions: Properly format the error message.

From: Bengt Richter <[email-masked]>
Date: Sun, 28 Jun 2020 20:23:13 +0200

Hi Maxim, Ricardo,

On +2020-06-28 00:25:28 -0400, Maxim Cournoyer wrote:
> Hello Bengt,
> 
> Bengt Richter <[email-masked]> writes:
> 
> 
> [...]
> 
> > What do you think of using (ice-9 match) to make a universal throwage-formatter,
> > with the idea of making readable top level code for how exceptions become messages on screen?
> >
> > I started a hack to explore the (throw 'whatever any ...) space, beginning like
> >
> > (use-modules (ice-9 match))
> > (define (make-exception-message key rest)
> >   (begin
> >     (let*((l (cons key rest)))
> >       (match l
> > 	     (('system-error subr message args data ...)
> > 	      ;; e.g. thrown with key 'system-error: ("open-fdes" "~A" ("No such file or directory") (2))
> > 	      (format #f (string-append "match-1: subr ~s threw '~s " message " sterror: ~s") subr key args (strerror (car (car data)))))
> >
> > 	     (('signal   any ...)
> >               ;; not yet implemented
> > 	      (format #f "match-2: <NYI:unix signal no> any: ~s" any))
> 
> Are you proposing to refactor (ice-9 exceptions) so that it's simpler to
> follow a condition message is formed for a given exception type?
> 
> Maxim

Well, there's probably no catching up with Ludo and Andy, judging from NEWS[1], specifically [2],
so practically speaking, no. I was just floating the idea :)

You may have noticed the "match-1" in e.g.
--8<---------------cut here---------------start------------->8---
> > 	      (format #f (string-append "match-1: subr ~s threw '~s " message " sterror: ~s") subr key args (strerror (car (car data)))))
--8<---------------cut here---------------end--------------->8---
above. The idea there was to find easily the exact format expression that did the output,
(using the tag, to be included per some debug flag or env, but otherwise doing standard error reporting.)

A simple sequence of matches makes it easy to include such debug tags and find them in the code.
(not always easy without tags, I found :)

I guess if match is sequentially implemented like an if-elif*-else chain, it could be too slow without
a generic dispatch to different specialized match sequences, like the dispatch being used now in

--8<---------------cut here---------------start------------->8---
define (convert-guile-exception key args)
  (let ((converter (assv-ref guile-exception-converters key)))
    (make-exception (or (and converter (converter key args))
                        (default-guile-exception-converter key args))
                    (make-exception-with-kind-and-args key args))))
--8<---------------cut here---------------end--------------->8---
┌────────────────────────────────────────────────────────────────────────┐
│ BTW, I wonder if using a hash table and hashv-ref instead off assv-ref │
│ would make any noticeable speed difference for anyone.                 │
└────────────────────────────────────────────────────────────────────────┘

The commit diffs on match sequences would become a revision history for the fixes that will probably
continue to be necessary, yet the simple surface syntax would be understandable by a newbie.

Actually, looking at the code, I think I will get away while I can,
before I start seeing udev rules in the mix :)
 
IRL, my next hack might be a (false-if-exception-with-report sexpr) that would ouput to (current-error-port)
if an exception does occur, besides returning #f like the plain false-if-exception. 
First a guaranteed (format (current-error-port) "key=<~s> args=~s\n" key args) seen by the handler, then
something more verbose and informative.

Just as a hacking tool, when I want to paper something over, but also want to know what's happening ;-)

Unfortunately, it probably won't help with syntax errors not finding closing quotes or parens and hitting eof ;/

Hope I haven't annoyed anyone.

[1] https://git.savannah.gnu.org/gitweb/?p=guile.git;a=blob_plain;f=NEWS;hb=5e1748f75128107e3a0707b66df5adb95d98437e
(I guess that's up to date, I git cloned the repo and first read the NEWS there.
LOTS going on ;-)

[2] Snip of part I wanted to indicate:
--8<---------------cut here---------------start------------->8---
** Reimplementation of exceptions

Since Guile's origins 25 years ago, `throw' and `catch' have been the
primary exception-handling primitives.  However these primitives have
two problems.  One is that it's hard to handle exceptions in a
structured way using `catch'.  Few people remember what the
corresponding `key' and `args' are that an exception handler would see
in response to a call to `error', for example.  In practice, this
results in more generic catch-all exception handling than one might
like.

The other problem is that `throw', `catch', and especially
`with-throw-handler' are quite unlike what the rest of the Scheme world
uses.  R6RS and R7RS, for example, have mostly converged on
SRFI-34-style `with-exception-handler' and `raise' primitives, and
encourage the use of SRFI-35-style structured exception objects to
describe the error.  Guile's R6RS layer incorporates an adapter between
`throw'/`catch' and structured exception handling, but it didn't apply
to SRFI-34/SRFI-35, and we would have to duplicate it for R7RS.

In light of these considerations, Guile has now changed to make
`with-exception-handler' and `raise-exception' its primitives for
exception handling and defined a hierarchy of R6RS-style exception types
in its core.  SRFI-34/35, R6RS, and the exception-handling components of
SRFI-18 (threads) have been re-implemented in terms of this core
functionality.  There is also a a compatibility layer that makes it so
that exceptions originating in `throw' can be handled by
`with-exception-hander', and vice-versa for `raise-exception' and
`catch'.

Generally speaking, users will see no difference.  The one significant
difference is that users of SRFI-34 will see more exceptions flowing
through their `with-exception-handler'/`guard' forms, because whereas
before they would only see exceptions thrown by SRFI-34, now they will
see exceptions thrown by R6RS, R7RS, or indeed `throw'.

Guile's situation is transitional.  Most exceptions are still signalled
via `throw'.  These will probably migrate over time to
`raise-exception', while preserving compatibility of course.

See "Exceptions" in the manual, for full details on the new API.
--8<---------------cut here---------------end--------------->8---

-- 
Regards,
Bengt Richter