diff options
| author | Nicolas Graves <ngraves@ngraves.fr> | 2026-06-16 11:58:34 +0200 |
|---|---|---|
| committer | Maxim Cournoyer <maxim@guixotic.coop> | 2026-08-27 10:59:20 +0900 |
| commit | 1bb9bdaf432d0fc8fa9675ba9f535a0043149a81 (patch) | |
| tree | a3fdbbf28f49a6b1968a81ec19e700e32c9c8da7 | |
| parent | 3c0ba3e223e641bc06e81267ecea39235fa786aa (diff) | |
guix: json-utils: Improve error handling.
* guix/build/json-utils.scm (&modify-json-error)
(&modify-json-invalid-field-value-error)
(&modify-json-missing-key-error): Add conditions.
(modify-json-fields): Use them.
* tests/json-utils.scm
("modify-json-fields, invalid field-path raises error"): Rewrite test
using test-error.
("modify-json-fields, missing key raises error"): Add test.
Change-Id: Ia12d31d90552f483997a38e23da4dc1fbac3afe9
Signed-off-by: Jelle Licht <jlicht@fsfe.org>
| -rw-r--r-- | guix/build/json-utils.scm | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/guix/build/json-utils.scm b/guix/build/json-utils.scm index 1a5cba54ab3..67025a5b1ae 100644 --- a/guix/build/json-utils.scm +++ b/guix/build/json-utils.scm | |||
| @@ -33,6 +33,8 @@ | |||
| 33 | #:use-module (json) | 33 | #:use-module (json) |
| 34 | #:use-module (srfi srfi-1) | 34 | #:use-module (srfi srfi-1) |
| 35 | #:use-module (srfi srfi-26) | 35 | #:use-module (srfi srfi-26) |
| 36 | #:use-module (srfi srfi-34) | ||
| 37 | #:use-module (srfi srfi-35) | ||
| 36 | #:export (with-atomic-json-file-replacement | 38 | #:export (with-atomic-json-file-replacement |
| 37 | modify-json | 39 | modify-json |
| 38 | modify-json-fields | 40 | modify-json-fields |
| @@ -41,7 +43,10 @@ | |||
| 41 | replace-fields | 43 | replace-fields |
| 42 | replace-json-fields | 44 | replace-json-fields |
| 43 | add-fields | 45 | add-fields |
| 44 | add-json-fields)) | 46 | add-json-fields |
| 47 | |||
| 48 | &modify-json-invalid-field-value-error | ||
| 49 | &modify-json-missing-key-error)) | ||
| 45 | 50 | ||
| 46 | ;;; | 51 | ;;; |
| 47 | ;;; JSON modification procedures | 52 | ;;; JSON modification procedures |
| @@ -110,6 +115,18 @@ as '(#:foo 1 #:bar 2)." | |||
| 110 | (warning (G_ "'modify-json' requires a file as the first argument~%")) | 115 | (warning (G_ "'modify-json' requires a file as the first argument~%")) |
| 111 | (apply modify-json* "package.json" args))))) | 116 | (apply modify-json* "package.json" args))))) |
| 112 | 117 | ||
| 118 | (define-condition-type &modify-json-error &error | ||
| 119 | modify-json-error?) | ||
| 120 | |||
| 121 | (define-condition-type &modify-json-invalid-field-value-error &modify-json-error | ||
| 122 | modify-json-invalid-field-value-error? | ||
| 123 | (field-path modify-json-invalid-field-value-error-field-path)) | ||
| 124 | |||
| 125 | (define-condition-type &modify-json-missing-key-error &modify-json-error | ||
| 126 | modify-json-missing-key-error? | ||
| 127 | (key modify-json-missing-key-error-key) | ||
| 128 | (data modify-json-missing-key-error-data)) | ||
| 129 | |||
| 113 | (define* (modify-json-fields fields field-modifier | 130 | (define* (modify-json-fields fields field-modifier |
| 114 | #:key | 131 | #:key |
| 115 | (field-path-mapper identity) | 132 | (field-path-mapper identity) |
| @@ -141,9 +158,13 @@ thrown if the exact field-path is not found in the data." | |||
| 141 | (string-split field-path #\.)) | 158 | (string-split field-path #\.)) |
| 142 | ((and (list? field-path) (every string? field-path)) | 159 | ((and (list? field-path) (every string? field-path)) |
| 143 | field-path) | 160 | field-path) |
| 144 | (else (error (format #f "\ | 161 | (else |
| 145 | invalid field value provided, expected string or list of strings, got ~s~%" | 162 | (raise (make-compound-condition |
| 146 | field-path)))))) | 163 | (condition (&modify-json-invalid-field-value-error |
| 164 | (field-path field-path))) | ||
| 165 | (formatted-message (G_ "\ | ||
| 166 | invalid field value provided, expected string or list of strings, got ~s~%") | ||
| 167 | field-path))))))) | ||
| 147 | (let loop ((data package) | 168 | (let loop ((data package) |
| 148 | (field-path field-path)) | 169 | (field-path field-path)) |
| 149 | (let* ((key (car field-path)) | 170 | (let* ((key (car field-path)) |
| @@ -153,8 +174,13 @@ invalid field value provided, expected string or list of strings, got ~s~%" | |||
| 153 | data))) | 174 | data))) |
| 154 | (if field-missing? | 175 | (if field-missing? |
| 155 | (if strict? | 176 | (if strict? |
| 156 | (error (format #f "key ~s was not found in data: ~y~%" | 177 | (raise (make-compound-condition |
| 157 | key data)) | 178 | (condition (&modify-json-missing-key-error |
| 179 | (key key) | ||
| 180 | (data data))) | ||
| 181 | (formatted-message | ||
| 182 | (G_ "key ~s was not found in data: ~y~%") | ||
| 183 | key data))) | ||
| 158 | data) | 184 | data) |
| 159 | (if (= (length field-path) 1) | 185 | (if (= (length field-path) 1) |
| 160 | (field-modifier field data key) | 186 | (field-modifier field data key) |
