From patchwork Sat Jun 18 21:38:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 40143 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 937BA27BBEA; Sat, 18 Jun 2022 22:39:22 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id D1FEF27BBE9 for ; Sat, 18 Jun 2022 22:39:21 +0100 (BST) Received: from localhost ([::1]:39846 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o2gA4-0005sU-Vi for patchwork@mira.cbaines.net; Sat, 18 Jun 2022 17:39:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48538) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o2g9l-0005rm-VY for guix-patches@gnu.org; Sat, 18 Jun 2022 17:39:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:56389) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o2g9l-0006im-N4 for guix-patches@gnu.org; Sat, 18 Jun 2022 17:39:01 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1o2g9l-0004Wj-Kh for guix-patches@gnu.org; Sat, 18 Jun 2022 17:39:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#56075] [PATCH 1/2] services: configuration: Report the location of field type errors. References: <20220618213640.25039-1-ludo@gnu.org> In-Reply-To: <20220618213640.25039-1-ludo@gnu.org> Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 18 Jun 2022 21:39:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56075 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 56075@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 56075-submit@debbugs.gnu.org id=B56075.165558832817374 (code B ref 56075); Sat, 18 Jun 2022 21:39:01 +0000 Received: (at 56075) by debbugs.gnu.org; 18 Jun 2022 21:38:48 +0000 Received: from localhost ([127.0.0.1]:50284 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1o2g9X-0004W8-V9 for submit@debbugs.gnu.org; Sat, 18 Jun 2022 17:38:48 -0400 Received: from eggs.gnu.org ([209.51.188.92]:35234) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1o2g9W-0004Vn-Ka for 56075@debbugs.gnu.org; Sat, 18 Jun 2022 17:38:47 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:45070) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o2g9R-0006fU-4H; Sat, 18 Jun 2022 17:38:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:Subject:To:From:in-reply-to: references; bh=SLGijAEP8SZ5PZxaqvol+Om7X6k2mVpt45+qQvZxwuM=; b=rHj+/MgRtzf4Hm qCrqfAwsBoHgsyPmjF/IGAFgSbF7veP4ZL7HPhE+xwed9fxLyoBTswQ2ZunYL7U8qEbD0t0bf0IKV zhnqRjSW/ihyUbgVUrMbQH/V+cHOCPmxRI2Dkpbr6hXDrcmP8Bi1NeaYvtVLbWhyMJ/68vwAKP/e4 IN4TXYL2CUwbifC7bunTjAyYcFajZ4g0h4pkxc+jutF+6NWeng5nr0KxSDkVj83qDt8dDx9uzWsaj +B2Dht5KfJPtdKE9pOmkiSi1c4eEkOsPlu89QrbLWicZDPpN3PIGeWRmr7FHti8hOerJfEiegL5+P wXsQsVk2U6w3y6FDn+fg==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:58587 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o2g9Q-00054e-Nx; Sat, 18 Jun 2022 17:38:40 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Sat, 18 Jun 2022 23:38:31 +0200 Message-Id: <20220618213832.25165-1-ludo@gnu.org> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches Previously field type errors would be reported in a non-standard way, and without any source location information. This fixes it. * gnu/services/configuration.scm (configuration-field-error): Add a 'loc' parameter and honor it. Use 'formatted-message' instead of plain 'format'. (define-configuration-helper)[field-sanitizer]: New procedure. Use it. Use STEM as the identifier of the syntactic constructor of the record type. Add a 'sanitize' property to each field. Remove now useless STEM macro that would call 'validate-configuration'. * gnu/services/mail.scm (serialize-listener-configuration): Adjust to new 'configuration-field-error' prototype. * tests/services/configuration.scm ("wrong type for a field"): New test. * po/guix/POTFILES.in: Add gnu/services/configuration.scm. --- gnu/services/configuration.scm | 55 +++++++++++++++++++++++++------- gnu/services/mail.scm | 2 +- po/guix/POTFILES.in | 1 + tests/services/configuration.scm | 12 +++++++ 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/gnu/services/configuration.scm b/gnu/services/configuration.scm index f6b20fb82b..c39ea5a02a 100644 --- a/gnu/services/configuration.scm +++ b/gnu/services/configuration.scm @@ -27,7 +27,8 @@ (define-module (gnu services configuration) #:use-module (guix records) #:use-module (guix gexp) #:use-module ((guix utils) #:select (source-properties->location)) - #:use-module ((guix diagnostics) #:select (formatted-message location-file)) + #:use-module ((guix diagnostics) + #:select (formatted-message location-file &error-location)) #:use-module ((guix modules) #:select (file-name->module-name)) #:use-module (guix i18n) #:autoload (texinfo) (texi-fragment->stexi) @@ -87,9 +88,17 @@ (define-condition-type &configuration-error &error (define (configuration-error message) (raise (condition (&message (message message)) (&configuration-error)))) -(define (configuration-field-error field val) - (configuration-error - (format #f "Invalid value for field ~a: ~s" field val))) +(define (configuration-field-error loc field value) + (raise (apply + make-compound-condition + (formatted-message (G_ "invalid value ~s for field '~a'") + value field) + (condition (&configuration-error)) + (if loc + (list (condition + (&error-location (location loc)))) + '())))) + (define (configuration-missing-field kind field) (configuration-error (format #f "~a configuration missing required field ~a" kind field))) @@ -210,9 +219,33 @@ (define (define-configuration-helper serialize? serializer-prefix syn) (id #'stem #'serialize- type)))))) #'(field-type ...) #'((custom-serializer ...) ...)))) + (define (field-sanitizer name pred) + ;; Define a macro for use as a record field sanitizer, where NAME + ;; is the name of the field and PRED is the predicate that tells + ;; whether a value is valid for this field. + #`(define-syntax #,(id #'stem #'validate- #'stem #'- name) + (lambda (s) + ;; Make sure the given VALUE, for field NAME, passes PRED. + (syntax-case s () + ((_ value) + (with-syntax ((name #'#,name) + (pred #'#,pred) + (loc (datum->syntax #'value + (syntax-source #'value)))) + #'(if (pred value) + value + (configuration-field-error + (and=> 'loc source-properties->location) + 'name value)))))))) + #`(begin + ;; Define field validation macros. + #,@(map field-sanitizer + #'(field ...) + #'(field-predicate ...)) + (define-record-type* #,(id #'stem #'< #'stem #'>) - #,(id #'stem #'% #'stem) + stem #,(id #'stem #'make- #'stem) #,(id #'stem #'stem #'?) (%location #,(id #'stem #'stem #'-location) @@ -220,10 +253,13 @@ (define-record-type* #,(id #'stem #'< #'stem #'>) source-properties->location)) (innate)) #,@(map (lambda (name getter def) - #`(#,name #,getter (default #,def))) + #`(#,name #,getter (default #,def) + (sanitize + #,(id #'stem #'validate- #'stem #'- name)))) #'(field ...) #'(field-getter ...) #'(field-default ...))) + (define #,(id #'stem #'stem #'-fields) (list (configuration-field (name 'field) @@ -240,12 +276,7 @@ (define #,(id #'stem #'stem #'-fields) '#,(id #'stem #'% #'stem) 'field) field-default))) (documentation doc)) - ...)) - (define-syntax-rule (stem arg (... ...)) - (let ((conf (#,(id #'stem #'% #'stem) arg (... ...)))) - (validate-configuration conf - #,(id #'stem #'stem #'-fields)) - conf)))))))) + ...)))))))) (define no-serialization ;syntactic keyword for 'define-configuration' '(no serialization)) diff --git a/gnu/services/mail.scm b/gnu/services/mail.scm index d99743ac31..c2fd4d8670 100644 --- a/gnu/services/mail.scm +++ b/gnu/services/mail.scm @@ -285,7 +285,7 @@ (define (serialize-listener-configuration field-name val) (serialize-fifo-listener-configuration field-name val)) ((inet-listener-configuration? val) (serialize-inet-listener-configuration field-name val)) - (else (configuration-field-error field-name val)))) + (else (configuration-field-error #f field-name val)))) (define (listener-configuration-list? val) (and (list? val) (and-map listener-configuration? val))) (define (serialize-listener-configuration-list field-name val) diff --git a/po/guix/POTFILES.in b/po/guix/POTFILES.in index 201e5dcc87..f50dd00422 100644 --- a/po/guix/POTFILES.in +++ b/po/guix/POTFILES.in @@ -4,6 +4,7 @@ gnu.scm gnu/packages.scm gnu/services.scm gnu/system.scm +gnu/services/configuration.scm gnu/services/shepherd.scm gnu/home/services.scm gnu/home/services/ssh.scm diff --git a/tests/services/configuration.scm b/tests/services/configuration.scm index 334a1e409b..cf3e504233 100644 --- a/tests/services/configuration.scm +++ b/tests/services/configuration.scm @@ -19,6 +19,7 @@ (define-module (tests services configuration) #:use-module (gnu services configuration) + #:use-module (guix diagnostics) #:use-module (guix gexp) #:use-module (srfi srfi-34) #:use-module (srfi srfi-64)) @@ -43,6 +44,17 @@ (define-configuration port-configuration 80 (port-configuration-port (port-configuration))) +(test-equal "wrong type for a field" + '("configuration.scm" 56 11) ;error location + (guard (c ((configuration-error? c) + (let ((loc (error-location c))) + (list (basename (location-file loc)) + (location-line loc) + (location-column loc))))) + (port-configuration + ;; This is line 55; the test relies on line/column numbers! + (port "This is not a number!")))) + (define-configuration port-configuration-cs (port (number 80) "The port number." empty-serializer))