[Libguestfs] [PATCH 7/9] ocaml: Avoid Warning 52 for Planner.plan function.

Richard W.M. Jones rjones at redhat.com
Wed Oct 4 12:56:28 UTC 2017


Change the Planner.plan function so it returns an optional type.  This
means it no longer raises Failure "plan" on error, so we can both
force the caller to deal with the error case and avoid Warning 52.
---
 builder/builder.ml         | 9 ++++-----
 common/mltools/planner.ml  | 4 ++--
 common/mltools/planner.mli | 5 ++---
 3 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/builder/builder.ml b/builder/builder.ml
index bf3f52f6a..1faf467c0 100644
--- a/builder/builder.ml
+++ b/builder/builder.ml
@@ -503,11 +503,10 @@ let main () =
   (* Plan how to create the disk image. *)
   message (f_"Planning how to build this image");
   let plan =
-    try plan ~max_depth:5 transitions itags ~must ~must_not
-    with
-      Failure "plan" ->
-        error (f_"no plan could be found for making a disk image with\nthe required size, format etc. This is a bug in libguestfs!\nPlease file a bug, giving the command line arguments you used.");
-  in
+    match plan ~max_depth:5 transitions itags ~must ~must_not with
+    | Some plan -> plan
+    | None ->
+       error (f_"no plan could be found for making a disk image with\nthe required size, format etc. This is a bug in libguestfs!\nPlease file a bug, giving the command line arguments you used.") in
 
   (* Print out the plan. *)
   if verbose () then (
diff --git a/common/mltools/planner.ml b/common/mltools/planner.ml
index 736cfee92..9730df843 100644
--- a/common/mltools/planner.ml
+++ b/common/mltools/planner.ml
@@ -50,7 +50,7 @@ let plan ?(max_depth = 10) transitions itags ~must ~must_not =
 
   (* Breadth-first search. *)
   let rec search depth paths =
-    if depth >= max_depth then failwith "plan"
+    if depth >= max_depth then None
     else (
       let paths =
         List.map (
@@ -76,7 +76,7 @@ let plan ?(max_depth = 10) transitions itags ~must ~must_not =
         (* Return the shortest path, but we have to reverse it because
          * we built it backwards.
          *)
-        List.rev ret
+        Some (List.rev ret)
     )
   in
 
diff --git a/common/mltools/planner.mli b/common/mltools/planner.mli
index 8cd1c51c9..88f9fb2ca 100644
--- a/common/mltools/planner.mli
+++ b/common/mltools/planner.mli
@@ -67,7 +67,7 @@ type ('name, 'value, 'task) transitions_function =
 val plan : ?max_depth:int -> ('name, 'value, 'task) transitions_function ->
            ('name, 'value) tags ->
            must: ('name, 'value) tags -> must_not: ('name, 'value) tags ->
-           ('name, 'value, 'task) plan
+           ('name, 'value, 'task) plan option
 (** Make a plan.
 
     [plan transitions itags goal_must goal_must_not] works out a
@@ -79,5 +79,4 @@ val plan : ?max_depth:int -> ('name, 'value, 'task) transitions_function ->
 
     The returned value is a {!plan}.
 
-    Raises [Failure "plan"] if no plan was found within [max_depth]
-    transitions. *)
+    Returns [None] if no plan was found within [max_depth] transitions. *)
-- 
2.13.2




More information about the Libguestfs mailing list