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

2020-06-28 Thread Bengt Richter
Hi Maxim, Ricardo,

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

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

2020-06-27 Thread Ricardo Wurmus


Hi Maxim,

>> here’s what I did in the REPL:
>>
>> scheme@(guile-user)> ,m (ice-9 exceptions)
>> scheme@(ice-9 exceptions)> (define (my/guile-system-error-converter key args)
>>   (apply (case-lambda
>>   ((subr msg-args msg errno . rest)

Here I changed the order: “msg-args” appears before “msg”.  I don’t know
why the converter that’s currently in Guile assumes that the message
comes first.

>> scheme@(ice-9 exceptions)> (set! guile-exception-converters (acons 
>> 'system-error my/guile-system-error-converter guile-exception-converters))

guile-exception-converters is a lookup table in (ice-9 exceptions).  It
associates error keys with converter procedures.  Since
canonicalize-path throws a 'system-error I chose to only update the
'system-error association.  I didn’t want to affect all the other
converter procedures that end up using the common converter; maybe they
should be affected — I don’t know because I don’t have any test cases
other than canonicalize-path.

> This brings embeds the definition of `guile-common-exceptions' into
> `guile-system-error-converter', with a single change:
>
> (make-exception-with-message msg) --> (apply make-exception-with-message
> msg msg-args)
>
> What is the magic I fail to see?

I cannot parse your sentence, so I’m not sure what you mean.

> Is this fix proper to be merged into the original
> guile-common-exceptions procedure?

I think we should add tests of different exceptions with different keys
to be sure that modifying the common handler doesn’t have any unexpected
side-effects.

-- 
Ricardo





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

2020-06-27 Thread Maxim Cournoyer
Hello Bengt,

Bengt Richter  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:  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





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

2020-06-27 Thread Maxim Cournoyer
Hello Ricardo!

Ricardo Wurmus  writes:

> Hi Maxim,
>
> here’s what I did in the REPL:
>
> scheme@(guile-user)> ,m (ice-9 exceptions)
> scheme@(ice-9 exceptions)> (define (my/guile-system-error-converter key args)
>   (apply (case-lambda
>   ((subr msg-args msg errno . rest)
>;; XXX TODO we should return a more specific error
>;; (usually an I/O error) as expected by R6RS programs.
>;; Unfortunately this often requires the 'filename' (or
>;; other?) which is not currently provided by the native
>;; Guile exceptions.
>  (make-exception
>   (make-external-error)
> (make-exception-with-origin subr)
> (apply make-exception-with-message msg)
> (make-exception-with-irritants msg-args)))
>   (_ (guile-external-error-converter key args)))
>  args))
> scheme@(ice-9 exceptions)> (set! guile-exception-converters (acons 
> 'system-error my/guile-system-error-converter guile-exception-converters))
> scheme@(ice-9 exceptions)> ,m (guile-user)
> scheme@(guile-user)> (guard (c ((message-condition? c)
> (format #t "message: ~a~%" (condition-message c
> (canonicalize-path "/doesntexist"))
> message: No such file or directory
> $11 = #t
> scheme@(guile-user)> 

I've tested that this indeed works, although I don't quite understand
how?

This brings embeds the definition of `guile-common-exceptions' into
`guile-system-error-converter', with a single change:

(make-exception-with-message msg) --> (apply make-exception-with-message
msg msg-args)

What is the magic I fail to see?

Is this fix proper to be merged into the original
guile-common-exceptions procedure?

Thank you!

Maxim





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

2020-06-25 Thread Bengt Richter
Hi Ricardo et al,

On +2020-06-25 12:04:27 +0200, Ricardo Wurmus wrote:
> 
> Hi Maxim,
> 
> here’s what I did in the REPL:
> 
> --8<---cut here---start->8---
> scheme@(guile-user)> ,m (ice-9 exceptions)
> scheme@(ice-9 exceptions)> (define (my/guile-system-error-converter key args)
>   (apply (case-lambda
>   ((subr msg-args msg errno . rest)
>;; XXX TODO we should return a more specific error
>;; (usually an I/O error) as expected by R6RS programs.
>;; Unfortunately this often requires the 'filename' (or
>;; other?) which is not currently provided by the native
>;; Guile exceptions.
>  (make-exception
>   (make-external-error)
> (make-exception-with-origin subr)
> (apply make-exception-with-message msg)
> (make-exception-with-irritants msg-args)))
>   (_ (guile-external-error-converter key args)))
>  args))
> scheme@(ice-9 exceptions)> (set! guile-exception-converters (acons 
> 'system-error my/guile-system-error-converter guile-exception-converters))
> scheme@(ice-9 exceptions)> ,m (guile-user)
> scheme@(guile-user)> (guard (c ((message-condition? c)
> (format #t "message: ~a~%" (condition-message c
> (canonicalize-path "/doesntexist"))
> message: No such file or directory
> $11 = #t
> scheme@(guile-user)> 
> --8<---cut here---end--->8---
> 
> -- 
> Ricardo

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

--8<---cut here---start->8---
(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:  any: ~s" any))
 
 (('keyword-argument-error subr message args data)
  ;; with-crossed-fingers...
  (format #f (string-append "match-3: subr ~s threw '~s " message) 
subr key args))
;; FIXME: string-append formats NAGI not a good idea, see a fix example below
  
 (('wrong-type-arg subr message (args ...) (data ...))
  ;; E.g.,  thrown with key 'wrong-type-arg: ("sqrt" "Wrong type 
argument in position ~A: ~S" (1 x) (x))
  (format #f "match-4: subr ~s threw '~s: ~s" subr key (format #f 
message args data)))
 
 (('out-of-range subr message (lo hi bad1) ((bad2)))
  ;; E.g., thrown with key 'out-of-range: (#f "Value out of range 
~S to ~S: ~S" (0 3 4) (4))
  (format #f "match-5: (internal) threw '~s: ~s" 'out-of-range 
(format #f message lo hi bad2)))

 (('unbound-variable #f message args data)
  ;; E.g. thrown with key 'unbound-variable: (#f "Unbound variable: 
~S" (foo) #f)
  (format #f (string-append "match-6: subr ~s threw '~s " message) 
#f key args)) ;; data))
;; FIXME: string-append formats NAGI
[...]
--8<---cut here---end--->8---

I made a guile hack that I could call from bash so I could type a line and 
(eval-string it) as a source of exceptions,
and found that I could get secondary exceptions from make-exception-message, so 
I wrapped that with a (catch ...)
something like

--8<---cut here---start->8---
(define verbose-exception-handler (lambda (k . rest )
  (begin
(format #t "thrown with key '~s: ~s\n" k rest)
(format #t "catch return=~a\n" (catch #t
(lambda () (make-exception-message  k  
rest))
(lambda (inner-key . inner-rest)
  (format #t "caught by inner handler: 
thrown with key '~s: ~s\n" inner-key inner-rest
;; (format #t "thrown with key '~s: ~s\n" k rest)
(newline)
 [...]
--8<---cut here---end--->8---

And using that like

--8<---cut here---start->8---
(define (wrap-main args)
  (begin
(display (catch #t
(lambda () (apply main-defaults args))
verbose-exception-handler

--8<---cut 

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

2020-06-25 Thread Ricardo Wurmus


Hi Maxim,

here’s what I did in the REPL:

--8<---cut here---start->8---
scheme@(guile-user)> ,m (ice-9 exceptions)
scheme@(ice-9 exceptions)> (define (my/guile-system-error-converter key args)
  (apply (case-lambda
  ((subr msg-args msg errno . rest)
   ;; XXX TODO we should return a more specific error
   ;; (usually an I/O error) as expected by R6RS programs.
   ;; Unfortunately this often requires the 'filename' (or
   ;; other?) which is not currently provided by the native
   ;; Guile exceptions.
   (make-exception
(make-external-error)
(make-exception-with-origin subr)
(apply make-exception-with-message msg)
(make-exception-with-irritants msg-args)))
  (_ (guile-external-error-converter key args)))
 args))
scheme@(ice-9 exceptions)> (set! guile-exception-converters (acons 
'system-error my/guile-system-error-converter guile-exception-converters))
scheme@(ice-9 exceptions)> ,m (guile-user)
scheme@(guile-user)> (guard (c ((message-condition? c)
  (format #t "message: ~a~%" (condition-message c
  (canonicalize-path "/doesntexist"))
message: No such file or directory
$11 = #t
scheme@(guile-user)> 
--8<---cut here---end--->8---

-- 
Ricardo





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

2020-06-20 Thread Maxim Cournoyer
Hello Bengt!

Bengt Richter  writes:

> Hi Maxim,
>
> tl;dr:
> Does module/ice-9/exceptions.scm use the default format?
> Maybe (use-modules (ice-9) format) will help get to the next bug ?? :)

Thanks for suggesting!  I tried but got the same result.

I'm now testing a slightly different version:

@@ -189,7 +189,10 @@
   ((subr msg margs . _)
(make-exception
 (make-exception-with-origin subr)
-(make-exception-with-message msg)
+(let ((msg (if (list? margs)
+   (apply simple-format #f msg margs)
+   msg)))
+(make-exception-with-message msg))
 (make-exception-with-irritants margs)))
   (_ (make-exception-with-irritants args)))
  args))

Maxim





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

2020-06-20 Thread Bengt Richter
Hi Maxim,

tl;dr:
Does module/ice-9/exceptions.scm use the default format?
Maybe (use-modules (ice-9) format) will help get to the next bug ?? :)

On +2020-06-20 01:46:13 -0400, maxim.courno...@gmail.com wrote:
> Maxim Cournoyer  writes:
> 
> > Hello,
> >
> > I had this problem in Guix where 'guix deploy my-config.scm' would
> > unhelpfully report an error like:
> >
> > guix deploy: error: failed to deploy my-host: ~A: ~S
> >
> > Digging a bit, I could reproduce at the REPL with:
> >
> > (guard (c ((message-condition? c)
> >(format #t "error: ~a~%" (condition-message c
> >   ;; This is what (canonicalize-path "/do/not/exist) ends up doing:
> >   (throw 'system-error "canonicalize-path" "~A" '("No such file or 
> > directory")))
> >
> > --> error: ~A
> 
> [...]
> 
> Unfortunately the previous patch breaks the tests, with errors like:
> 
> ERROR: bytevectors.test: Datum Syntax: incorrect prefix - arguments: 
> ((wrong-type-arg "apply" "Apply to non-list: ~S" (#\i) (#\i)))
> 
> I'm out of ideas for now, I last tried:
> 
> --8<---cut here---start->8---
> modified   module/ice-9/exceptions.scm
> @@ -189,7 +189,10 @@
>((subr msg margs . _)
> (make-exception
>  (make-exception-with-origin subr)
> -(make-exception-with-message msg)
> +(let ((msg (if (null? margs)
> +   msg
> +   (apply simple-format #f msg margs
> +(make-exception-with-message msg))
>  (make-exception-with-irritants margs)))
>(_ (make-exception-with-irritants args)))
>   args))
> --8<---cut here---end--->8---
> 
> To the same effect.
> 
> Maxim
> 
> 
> 

HTH
-- 
Regards,
Bengt Richter





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

2020-06-19 Thread maxim . cournoyer
Maxim Cournoyer  writes:

> Hello,
>
> I had this problem in Guix where 'guix deploy my-config.scm' would
> unhelpfully report an error like:
>
> guix deploy: error: failed to deploy my-host: ~A: ~S
>
> Digging a bit, I could reproduce at the REPL with:
>
> (guard (c ((message-condition? c)
>(format #t "error: ~a~%" (condition-message c
>   ;; This is what (canonicalize-path "/do/not/exist) ends up doing:
>   (throw 'system-error "canonicalize-path" "~A" '("No such file or 
> directory")))
>
> --> error: ~A

[...]

Unfortunately the previous patch breaks the tests, with errors like:

ERROR: bytevectors.test: Datum Syntax: incorrect prefix - arguments: 
((wrong-type-arg "apply" "Apply to non-list: ~S" (#\i) (#\i)))

I'm out of ideas for now, I last tried:

--8<---cut here---start->8---
modified   module/ice-9/exceptions.scm
@@ -189,7 +189,10 @@
   ((subr msg margs . _)
(make-exception
 (make-exception-with-origin subr)
-(make-exception-with-message msg)
+(let ((msg (if (null? margs)
+   msg
+   (apply simple-format #f msg margs
+(make-exception-with-message msg))
 (make-exception-with-irritants margs)))
   (_ (make-exception-with-irritants args)))
  args))
--8<---cut here---end--->8---

To the same effect.

Maxim





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

2020-06-19 Thread Maxim Cournoyer
Hello,

I had this problem in Guix where 'guix deploy my-config.scm' would
unhelpfully report an error like:

guix deploy: error: failed to deploy my-host: ~A: ~S

Digging a bit, I could reproduce at the REPL with:

--8<---cut here---start->8---
(guard (c ((message-condition? c)
   (format #t "error: ~a~%" (condition-message c
  ;; This is what (canonicalize-path "/do/not/exist) ends up doing:
  (throw 'system-error "canonicalize-path" "~A" '("No such file or directory")))

--> error: ~A
--8<---cut here---end--->8---

It seems our native -> srfi-34 style exception converter should populate
the message field with a formatted message, given that's what happens to
present errors as explained in libguile/error.c:

When an error is reported,\n
"these are replaced by formatting the corresponding members of\n"
"@var{args}: @code{~A} (was @code{%s} in older versions of\n"
"Guile) formats using @code{display} and @code{~S}

I'm not sure about the second ~S that appeared in the Guix output;
possibly the exception got re-thrown and suffered from a slightly
different conversion problem?

Anyway, the simple patch attached should fix the "~A" exception message.

Thank you,

Maxim

>From adaa2f66fec7684e9e65491158afc5923613e3da Mon Sep 17 00:00:00 2001
From: Maxim Cournoyer 
Date: Fri, 19 Jun 2020 14:30:05 -0400
Subject: [PATCH] ice-9: exceptions: Properly format the error message.

Before this change, native exceptions such as system errors caught with
srfi-34's `guard' would be converted to an exception with its message
unhelpfully set to "~A".

Thus, apply the message arguments to the message format to derive a human
readable exception message.

* module/ice-9/exceptions.scm (guile-common-exceptions): Format the
message string using its arguments.
---
 module/ice-9/exceptions.scm | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/module/ice-9/exceptions.scm b/module/ice-9/exceptions.scm
index 143e7aa3e..c990790e9 100644
--- a/module/ice-9/exceptions.scm
+++ b/module/ice-9/exceptions.scm
@@ -189,7 +189,7 @@
   ((subr msg margs . _)
(make-exception
 (make-exception-with-origin subr)
-(make-exception-with-message msg)
+(make-exception-with-message (apply format #f msg margs))
 (make-exception-with-irritants margs)))
   (_ (make-exception-with-irritants args)))
  args))
-- 
2.26.2