From 234c7a0cb2403a87f7e40bcd978eb12d98535e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Fri, 15 Jul 2022 11:13:11 +0200 Subject: [PATCH] Refactor not found logic --- lib/builder_web.ml | 39 ++++++++++++++++++++++----------------- lib/model.ml | 2 +- lib/model.mli | 2 +- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/builder_web.ml b/lib/builder_web.ml index 07ed2d6..0db497e 100644 --- a/lib/builder_web.ml +++ b/lib/builder_web.ml @@ -85,6 +85,13 @@ let if_error Lwt_result.fail (message, status) | Ok _ as r -> Lwt.return r +let not_found_error r = + let* r = r in + match r with + | Error `Not_found -> + Lwt_result.fail ("Resource not found", `Not_Found) + | Ok _ as r -> Lwt.return r + let get_uuid s = Lwt.return (if String.length s = 36 then @@ -99,11 +106,11 @@ let main_binary_of_uuid uuid db = |> if_error "Error getting job build" ~log:(fun e -> Log.warn (fun m -> m "Error getting job build: %a" pp_error e)) >>= fun (_id, build) -> - match build.Builder_db.Build.main_binary with - | None -> Lwt_result.fail ("Resource not found", `Not_Found) - | Some main_binary -> - Model.build_artifact_by_id main_binary db - |> if_error "Error getting main binary" + Model.not_found build.Builder_db.Build.main_binary + |> not_found_error + >>= fun main_binary -> + Model.build_artifact_by_id main_binary db + |> if_error "Error getting main binary" module Viz_aux = struct @@ -203,12 +210,11 @@ module Viz_aux = struct artifacts in begin - match debug_binary with - | None -> Lwt_result.fail ("Error getting debug-binary", `Not_Found) - | Some debug_binary -> - debug_binary.sha256 - |> hex - |> Lwt_result.return + Model.not_found debug_binary + |> not_found_error >>= fun debug_binary -> + debug_binary.sha256 + |> hex + |> Lwt_result.return end | `Dependencies -> let opam_switch = @@ -216,12 +222,11 @@ module Viz_aux = struct (fun p -> Fpath.(equal (v "opam-switch") (base p.localpath))) artifacts in - match opam_switch with - | None -> Lwt_result.fail ("Error getting opam-switch", `Not_Found) - | Some opam_switch -> - opam_switch.sha256 - |> hex - |> Lwt_result.return + Model.not_found opam_switch + |> not_found_error >>= fun opam_switch -> + opam_switch.sha256 + |> hex + |> Lwt_result.return let try_load_cached_visualization ~cachedir ~uuid viz_typ db = Lwt.return (get_viz_version_from_dirs ~cachedir ~viz_typ) diff --git a/lib/model.ml b/lib/model.ml index d7f9cc7..3e796ba 100644 --- a/lib/model.ml +++ b/lib/model.ml @@ -15,7 +15,7 @@ let pp_error ppf = function Caqti_error.pp ppf e let not_found = function - | None -> Lwt.return (Error `Not_found :> (_, [> error ]) result) + | None -> Lwt_result.fail `Not_found | Some v -> Lwt_result.return v let staging datadir = Fpath.(datadir / "_staging") diff --git a/lib/model.mli b/lib/model.mli index 6982c61..c81857e 100644 --- a/lib/model.mli +++ b/lib/model.mli @@ -2,7 +2,7 @@ type error = [ Caqti_error.call_or_retrieve | `Not_found | `File_error of Fpath. val pp_error : Format.formatter -> error -> unit -val not_found : 'a option -> ('a, [> error ]) result Lwt.t +val not_found : 'a option -> ('a, [> `Not_found ]) result Lwt.t val staging : Fpath.t -> Fpath.t