From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Bengt Richter Newsgroups: gmane.lisp.guile.bugs Subject: bug#41956: [PATCH] ice-9: exceptions: Properly format the error message. Date: Thu, 25 Jun 2020 18:33:41 +0200 Message-ID: <20200625163341.GA4622@LionPure> References: <87eeqad9m9.fsf@gmail.com> <87wo42mgre.fsf@hurd.i-did-not-set--mail-host-address--so-tickle-me> <20200620183334.GA9490@LionPure> <87r1u9m62o.fsf@gmail.com> <87bll7qx5g.fsf@elephly.net> Reply-To: Bengt Richter Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="25351"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/1.10.1 (2018-07-13) Cc: 41956@debbugs.gnu.org To: Ricardo Wurmus Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Thu Jun 25 18:35:11 2020 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1joUqD-0006QT-8F for guile-bugs@m.gmane-mx.org; Thu, 25 Jun 2020 18:35:09 +0200 Original-Received: from localhost ([::1]:54372 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1joUqC-0001uw-7G for guile-bugs@m.gmane-mx.org; Thu, 25 Jun 2020 12:35:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58844) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1joUq6-0001t8-JH for bug-guile@gnu.org; Thu, 25 Jun 2020 12:35:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:57681) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1joUq6-0004lz-8G for bug-guile@gnu.org; Thu, 25 Jun 2020 12:35:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1joUq6-0003Wo-4i for bug-guile@gnu.org; Thu, 25 Jun 2020 12:35:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Bengt Richter Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Thu, 25 Jun 2020 16:35:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 41956 X-GNU-PR-Package: guile X-GNU-PR-Keywords: patch Original-Received: via spool by 41956-submit@debbugs.gnu.org id=B41956.159310284613489 (code B ref 41956); Thu, 25 Jun 2020 16:35:02 +0000 Original-Received: (at 41956) by debbugs.gnu.org; 25 Jun 2020 16:34:06 +0000 Original-Received: from localhost ([127.0.0.1]:40994 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1joUpB-0003VV-Ta for submit@debbugs.gnu.org; Thu, 25 Jun 2020 12:34:06 -0400 Original-Received: from imta-38.everyone.net ([216.200.145.38]:57392) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1joUpA-0003VN-6w for 41956@debbugs.gnu.org; Thu, 25 Jun 2020 12:34:04 -0400 Original-Received: from pps.filterd (omta003.sj2.proofpoint.com [127.0.0.1]) by imta-38.everyone.net (8.16.0.27/8.16.0.27) with SMTP id 05PGPGjK028342; Thu, 25 Jun 2020 09:34:02 -0700 X-Eon-Originating-Account: Nb8qXJ-a_WuxPfaGwzA-bTdHjw2r4YQKGUL8xz9HhFE X-Eon-Dm: m0116953.ppops.net Original-Received: by m0116953.mta.everyone.net (EON-AUTHRELAY2 - 5a81d29d) id m0116953.5ef25221.3a04d; Thu, 25 Jun 2020 09:33:51 -0700 X-Eon-Sig: AQMHrIJe9NHvUVsXjQIAAAAD,b1bf74c4975ecd004bfa194511b5e8a9 X-Eip: wyZLLpXTz6TeNKWPMjqp1zcjrVN3pe8e8RkS9p4qkCo Content-Disposition: inline In-Reply-To: <87bll7qx5g.fsf@elephly.net> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.216, 18.0.687 definitions=2020-06-25_11:2020-06-25, 2020-06-25 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1034 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-2004280000 definitions=main-2006250103 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: "bug-guile" Xref: news.gmane.io gmane.lisp.guile.bugs:9831 Archived-At: 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 here---------------end--------------->8--- (main-defaults sets up missing args and calls main with fixed args plus opt rest) I'm wondering if some of the obscure error messages I've encountered might be secondary, and could be clarified by wrapping the primary exception formatting similarly, (adding apropriate hint). WDYT? And is (ice-9 match) too massive to use for memory-limited platforms? BTW, does guile have a Content-type X-...-like standard for naming throw keys?, e.g. 'X-longjump-result-delivery if someone should want to use throw for non-"standard (?? :)" purposes? -- Regards, Bengt Richter