unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Bengt Richter <bokr@bokr.com>
To: Maxim Cournoyer <maxim.cournoyer@gmail.com>
Cc: 41956@debbugs.gnu.org
Subject: bug#41956: [PATCH] ice-9: exceptions: Properly format the error message.
Date: Sun, 28 Jun 2020 20:23:13 +0200	[thread overview]
Message-ID: <20200628182313.GA2809@LionPure> (raw)
In-Reply-To: <87tuyvlsuf.fsf@gmail.com>

Hi Maxim, Ricardo,

On +2020-06-28 00:25:28 -0400, Maxim Cournoyer wrote:
> Hello Bengt,
> 
> Bengt Richter <bokr@bokr.com> 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





  reply	other threads:[~2020-06-28 18:23 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-19 21:33 bug#41956: [PATCH] ice-9: exceptions: Properly format the error message Maxim Cournoyer
2020-06-20  5:46 ` maxim.cournoyer
2020-06-20 18:33   ` Bengt Richter
2020-06-21  3:49     ` Maxim Cournoyer
2020-06-25 10:04       ` Ricardo Wurmus
2020-06-25 16:33         ` Bengt Richter
2020-06-28  4:25           ` Maxim Cournoyer
2020-06-28 18:23             ` Bengt Richter [this message]
2020-06-28  4:17         ` Maxim Cournoyer
2020-06-28  4:31           ` Ricardo Wurmus
2021-06-02  7:32 ` bug#41956: is this still current ? Adriano Peluso
2023-11-10  4:17   ` Maxim Cournoyer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200628182313.GA2809@LionPure \
    --to=bokr@bokr.com \
    --cc=41956@debbugs.gnu.org \
    --cc=maxim.cournoyer@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).