From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id OOkYAgZGrmJV6gAAbAwnHQ (envelope-from ) for ; Sat, 18 Jun 2022 23:39:18 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id 0KUtAgZGrmLVJAAAauVa8A (envelope-from ) for ; Sat, 18 Jun 2022 23:39:18 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 73E1524BEE for ; Sat, 18 Jun 2022 23:39:17 +0200 (CEST) Received: from localhost ([::1]:39846 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o2gA0-0005sU-Al for larch@yhetil.org; Sat, 18 Jun 2022 17:39:16 -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 Content-Transfer-Encoding: 8bit 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+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1655588357; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=SLGijAEP8SZ5PZxaqvol+Om7X6k2mVpt45+qQvZxwuM=; b=rFAqZ3tajPEuSNvEpNutsECFhTuzEbt1hvwGPqlSdRRmTBI/uxu7iCXWP4i9Hq9EJI9Ii8 SbxdZ0YJ5Ay1sxNxgSxndOKAtA7GkZEbWGakf5d5lgpztsnFltHwQnHL19o7HDKtc0UrY8 O0NIRa5e+mZAQMD7+vYSGyXwGFiPBSPmCNpPKfKGLQiKjXYScu48pIEiRiBjtS2WPipaE6 wb+VgCp7iGylBXzdkGF38NaKlyTKdBGVxDeuxxDv1KmlTSW+25S4lTyXCww+zDl0DSB3Ox uOxLVgymEqHolK3DvOXsiMV+RxKwIO3+y2xzfONjUII0Iw3zdBi+77eS2K/drw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1655588357; a=rsa-sha256; cv=none; b=rEB2pDi09bo0I5LvGvAoDU3+k2JbbdJauRqrAwShihRT3mBsCIU2bHNzzTlv/HEbqhnYnU EV8UJTL+xAowNVT4iZdt0nwBzGS2HVsWe+VVNPKmOiFxJKI5RLIhEV/Ug3uCe9mUTZr4Et Nidjhsh0TAPRxtuumWN9QXyN+LK5hFFdB/xgv2XepbifEBza5RTT026J8j84EM9SHgQsWd /NTN928bs9LFuEgtLQiJXie/BXfb0nz4JI3NUoINQbT2e8ENaSCtqH29wN+tnoq0VYR6hi H/tOW26qCj15SuwUMclEveHKbd6UN4xnIXL+SCBZMCP4HqUGGupHkYNJq7aw2A== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b="rHj+/MgR"; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -4.09 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b="rHj+/MgR"; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 73E1524BEE X-Spam-Score: -4.09 X-Migadu-Scanner: scn0.migadu.com X-TUID: UQ95yz8GQ6TQ 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)) -- 2.36.1