unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Julien Lepiller <julien@lepiller.eu>
To: 49671@debbugs.gnu.org
Subject: [bug#49671] [PATCH v3] guix: records: Improve error reporting.
Date: Mon, 22 Nov 2021 03:40:22 +0100	[thread overview]
Message-ID: <20211122034022.152daf55@tachikoma.lepiller.eu> (raw)
In-Reply-To: <20211031030635.520a3c6c@tachikoma.lepiller.eu>

[-- Attachment #1: Type: text/plain, Size: 432 bytes --]

Here is another improvement compared to v2. This time there are two
patches: the first adds support for &syntax in (guix ui), and will
print something like

in form: <pretty-printed-form>

where "in form" is in green.

The second patch is very similar to v2, but will now also raise a
&syntax condition, so it can be pretty-printed. The previous issue
where I printed #<syntax ...> is fixed, I simply forgot a syntax->datum.

WDYT?

[-- Attachment #2: 0001-guix-ui-Print-syntax-errors.patch --]
[-- Type: text/x-patch, Size: 3252 bytes --]

From d1d857ea0ca76f1a917382777f57871e50026df3 Mon Sep 17 00:00:00 2001
Message-Id: <d1d857ea0ca76f1a917382777f57871e50026df3.1637548566.git.julien@lepiller.eu>
From: Julien Lepiller <julien@lepiller.eu>
Date: Mon, 22 Nov 2021 02:54:06 +0100
Subject: [PATCH 1/2] guix: ui: Print syntax errors.

* guix/ui.scm (display-syntax): New procedure.
(call-with-error-handling, report-load-error): Print syntax errors.
---
 guix/ui.scm | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/guix/ui.scm b/guix/ui.scm
index bd999103ff..79aab2db84 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -69,9 +69,11 @@ (define-module (guix ui)
   #:use-module (srfi srfi-31)
   #:use-module (srfi srfi-34)
   #:use-module (srfi srfi-35)
+  #:use-module (ice-9 exceptions)
   #:autoload   (ice-9 ftw)  (scandir)
   #:use-module (ice-9 match)
   #:use-module (ice-9 format)
+  #:use-module (ice-9 pretty-print)
   #:use-module (ice-9 regex)
   #:autoload   (ice-9 popen) (open-pipe* close-pipe)
   #:autoload   (system repl repl)  (start-repl)
@@ -315,6 +317,19 @@ (define* (display-hint message #:optional (port (current-error-port)))
      (texi->plain-text message))
    port))
 
+(define %syntax-color (color BOLD GREEN))
+
+(define* (display-syntax form #:optional (port (current-error-port)))
+  "Display FORM, an sexp, to PORT"
+  (define colorize
+    (if (color-output? port)
+        (lambda (str)
+          (colorize-string str %syntax-color))
+        identity))
+
+  (display (colorize (G_ "in form: ")) port)
+  (pretty-print form port))
+
 (define* (report-unbound-variable-error args #:key frame)
   "Return the given unbound-variable error, where ARGS is the list of 'throw'
 arguments."
@@ -398,6 +413,9 @@ (define* (report-load-error file args #:optional frame)
                    (formatted-message-arguments obj)))
            (else
             (report-error (G_ "exception thrown: ~s~%") obj)))
+     (when (syntax-error? obj)
+       (let ((form (or (syntax-error-subform obj) (syntax-error-form obj))))
+         (display-syntax form)))
      (when (fix-hint? obj)
        (display-hint (condition-fix-hint obj))))
     ((key args ...)
@@ -801,6 +819,9 @@ (define (call-with-error-handling thunk)
                      (and (error-location? c) (error-location c))
                      (gettext (formatted-message-string c) %gettext-domain)
                      (formatted-message-arguments c))
+              (when (syntax-error? c)
+                (let ((form (or (syntax-error-subform c) (syntax-error-form c))))
+                  (display-syntax form)))
               (when (fix-hint? c)
                 (display-hint (condition-fix-hint c)))
               (exit 1))
@@ -826,6 +847,9 @@ (define (call-with-error-handling thunk)
               (report-error (and (error-location? c) (error-location c))
                             (G_ "~a~%")
                             (gettext (condition-message c) %gettext-domain))
+              (when (syntax-error? c)
+                (let ((form (or (syntax-error-subform c) (syntax-error-form c))))
+                  (display-syntax form)))
               (when (fix-hint? c)
                 (display-hint (condition-fix-hint c)))
               (exit 1)))
-- 
2.33.1


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-guix-records-Improve-error-reporting.patch --]
[-- Type: text/x-patch, Size: 5267 bytes --]

From 2eebd1113a40fc6e7018975d3696546de584e4a0 Mon Sep 17 00:00:00 2001
Message-Id: <2eebd1113a40fc6e7018975d3696546de584e4a0.1637548566.git.julien@lepiller.eu>
In-Reply-To: <d1d857ea0ca76f1a917382777f57871e50026df3.1637548566.git.julien@lepiller.eu>
References: <d1d857ea0ca76f1a917382777f57871e50026df3.1637548566.git.julien@lepiller.eu>
From: Julien Lepiller <julien@lepiller.eu>
Date: Sun, 31 Oct 2021 02:58:14 +0100
Subject: [PATCH 2/2] guix: records: Improve error reporting.

* guix/records.scm (report-invalid-field-specifier): Handle various
invalidity causes separately.
---
 guix/records.scm | 56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)

diff --git a/guix/records.scm b/guix/records.scm
index ed94c83dac..eeb5908844 100644
--- a/guix/records.scm
+++ b/guix/records.scm
@@ -1,6 +1,7 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2018 Mark H Weaver <mhw@netris.org>
+;;; Copyright © 2021 Julien Lepiller <julien@lepiller.eu>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -21,9 +22,13 @@ (define-module (guix records)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-9)
   #:use-module (srfi srfi-26)
+  #:use-module (srfi srfi-35)
+  #:use-module (ice-9 exceptions)
   #:use-module (ice-9 match)
   #:use-module (ice-9 regex)
   #:use-module (ice-9 rdelim)
+  #:use-module (guix diagnostics)
+  #:use-module (guix ui)
   #:autoload (system base target) (target-most-positive-fixnum)
   #:export (define-record-type*
             this-record
@@ -83,10 +88,53 @@ (define-syntax record-error
          ;; WEIRD may be an identifier, thus lacking source location info, and
          ;; BINDINGS is a list, also lacking source location info.  Hopefully
          ;; PARENT-FORM provides source location info.
-         (apply syntax-violation name "invalid field specifier"
-                (if parent-form
-                    (list parent-form #'weird)
-                    (list #'weird)))))))
+         (let* ((weird-properties (source-properties #'weird))
+                (parent-properties (and parent-form (syntax-source parent-form)))
+                (form parent-form)
+                (location (source-properties->location
+                            (or (and (not (null? weird-properties)) weird-properties)
+                                (and (not (null? parent-properties)) parent-properties)
+                                '()))))
+           (syntax-case #'weird ()
+             (()                             ;the empty list
+              (raise-exception
+                (condition
+                  (&message (message (G_ "invalid field specifier.")))
+                  (&syntax (form form) (subform (syntax->datum #'weird)))
+                  (&error-location (location location))
+                  (&fix-hint (hint (G_ "The format of a field is `(field value)'"))))))
+             (((field ...) _ ...)            ;a list whose first element is a list
+              (raise-exception
+                (condition
+                  (&message (message (G_ "invalid field name.")))
+                  (&syntax (form form) (subform (map syntax->datum #'(field ...))))
+                  (&error-location (location location)))))
+             ((field)                        ;a list with one element
+              (raise-exception
+                (condition
+                  (&message (message (G_ "missing value in field specifier.")))
+                  (&syntax (form form) (subform (syntax->datum #'weird)))
+                  (&error-location (location location))
+                  (&fix-hint (hint (format #f (G_ "The field is missing a value: `(~a <value>)'.")
+                                           (syntax->datum #'field)))))))
+             ((field value ...)              ;any other list
+              (raise-exception
+                (condition
+                  (&message (message (G_ "multiple values in field specifier.")))
+                  (&syntax (form form) (subform (syntax->datum #'weird)))
+                  (&error-location (location location))
+                  (&fix-hint (hint (format #f (G_ "~a values were associated with \
+field `~a'.  Perhaps the additional values were intended to be other field \
+specifiers.  This usually indicates missing or misplaced parenthesis.")
+                                           (length #'(value ...))
+                                           (syntax->datum #'field)))))))
+             (field                          ;not a list
+              (raise-exception
+                (condition
+                  (&message (message (G_ "invalid field specifier.")))
+                  (&syntax (form form) (subform (syntax->datum #'weird)))
+                  (&error-location (location location))
+                  (&fix-hint (hint (G_ "The format of a field is `(field value)'"))))))))))))
 
   (define (report-duplicate-field-specifier name ctor)
     "Report the first duplicate identifier among the bindings in CTOR."
-- 
2.33.1


  reply	other threads:[~2021-11-22  2:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-20 23:40 [bug#49671] [PATCH] guix: records: Improve error reporting Julien Lepiller
2021-07-21 19:21 ` Sarah Morgensen
2021-08-04 15:19 ` Ludovic Courtès
2021-10-31  2:06 ` Julien Lepiller
2021-11-22  2:40   ` Julien Lepiller [this message]
2022-07-17  6:23     ` [bug#49671] [PATCH v3] " Julien Lepiller

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://guix.gnu.org/

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

  git send-email \
    --in-reply-to=20211122034022.152daf55@tachikoma.lepiller.eu \
    --to=julien@lepiller.eu \
    --cc=49671@debbugs.gnu.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

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).