diff mbox series

[bug#60802,1/2] platforms: Raise an exception when no suitable platform is found.

Message ID 20230114030817.23959-2-maxim.cournoyer@gmail.com
State New
Headers show
Series Remove unsupported u-boot-malta package | expand

Commit Message

Maxim Cournoyer Jan. 14, 2023, 3:08 a.m. UTC
This was motivated by #60786, which produced a cryptic, hard to understand
backtrace.

* guix/platform.scm (&platform-not-found-error): New exception type.
(make-platform-not-found-error): New exception constructor.
(platform-not-found-error?): New predicate.
(lookup-platform-by-system): Raise an exception when no platform is found.
Update documentation.
(lookup-platform-by-target): Likewise.
(lookup-platform-by-target-or-system): Likewise.
---

 gnu/packages/bootstrap.scm |  2 +-
 guix/platform.scm          | 44 +++++++++++++++++++++++++++-----------
 2 files changed, 33 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/gnu/packages/bootstrap.scm b/gnu/packages/bootstrap.scm
index d2914fb5a7..772be7e5e4 100644
--- a/gnu/packages/bootstrap.scm
+++ b/gnu/packages/bootstrap.scm
@@ -315,7 +315,7 @@  (define* (glibc-dynamic-linker
                                  (%current-system))))
   "Return the name of Glibc's dynamic linker for SYSTEM."
   ;; See the 'SYSDEP_KNOWN_INTERPRETER_NAMES' cpp macro in libc.
-  (let ((platform (lookup-platform-by-system system)))
+  (let ((platform (false-if-exception (lookup-platform-by-system system))))
     (cond
      ((platform? platform)
       (platform-glibc-dynamic-linker platform))
diff --git a/guix/platform.scm b/guix/platform.scm
index f873913fe0..c2be66e227 100644
--- a/guix/platform.scm
+++ b/guix/platform.scm
@@ -1,5 +1,6 @@ 
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2021 Mathieu Othacehe <othacehe@gnu.org>
+;;; Copyright © 2022 Maxim Cournoyer <maxim.cournoyer@gmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -21,6 +22,7 @@  (define-module (guix platform)
   #:use-module (guix memoization)
   #:use-module (guix records)
   #:use-module (guix ui)
+  #:use-module (ice-9 exceptions)
   #:use-module (srfi srfi-1)
   #:export (platform
             platform?
@@ -29,6 +31,10 @@  (define-module (guix platform)
             platform-linux-architecture
             platform-glibc-dynamic-linker
 
+            &platform-not-found-error
+            make-platform-not-found-error
+            platform-not-found-error?
+
             platform-modules
             platforms
             lookup-platform-by-system
@@ -70,6 +76,14 @@  (define-record-type* <platform> platform make-platform
                         (default #false))
   (glibc-dynamic-linker platform-glibc-dynamic-linker))
 
+
+;;;
+;;; Exception types.
+;;;
+(define-exception-type &platform-not-found-error &error
+  make-platform-not-found-error platform-not-found-error?)
+
+
 
 ;;;
 ;;; Platforms.
@@ -94,23 +108,29 @@  (define platforms
                                    (platform-modules)))))
 
 (define (lookup-platform-by-system system)
-  "Return the platform corresponding to the given SYSTEM."
-  (find (lambda (platform)
-          (let ((s (platform-system platform)))
-            (and (string? s) (string=? s system))))
-        (platforms)))
+  "Return the platform corresponding to the given SYSTEM.  Raise
+&PLATFORM-NOT-FOUND-ERROR when no platform could be found."
+  (or (find (lambda (platform)
+              (let ((s (platform-system platform)))
+                (and (string? s) (string=? s system))))
+            (platforms))
+      (raise-exception (make-platform-not-found-error))))
 
 (define (lookup-platform-by-target target)
-  "Return the platform corresponding to the given TARGET."
-  (find (lambda (platform)
-          (let ((t (platform-target platform)))
-            (and (string? t) (string=? t target))))
-        (platforms)))
+  "Return the platform corresponding to the given TARGET.  Raise
+&PLATFORM-NOT-FOUND-ERROR when no platform could be found."
+  (or (find (lambda (platform)
+              (let ((t (platform-target platform)))
+                (and (string? t) (string=? t target))))
+            (platforms))
+      (raise-exception (make-platform-not-found-error))))
 
 (define (lookup-platform-by-target-or-system target-or-system)
-  "Return the platform corresponding to the given TARGET or SYSTEM."
+  "Return the platform corresponding to the given TARGET or SYSTEM.  Raise
+&PLATFORM-NOT-FOUND-ERROR when no platform could be found."
   (or (lookup-platform-by-target target-or-system)
-      (lookup-platform-by-system target-or-system)))
+      (lookup-platform-by-system target-or-system)
+      (raise-exception (make-platform-not-found-error))))
 
 (define (platform-system->target system)
   "Return the target matching the given SYSTEM if it exists or false