Refactor not found logic

This commit is contained in:
Reynir Björnsson 2022-07-15 11:13:11 +02:00
parent 9416e0552d
commit 234c7a0cb2
3 changed files with 24 additions and 19 deletions

View file

@ -85,6 +85,13 @@ let if_error
Lwt_result.fail (message, status) Lwt_result.fail (message, status)
| Ok _ as r -> Lwt.return r | 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 = let get_uuid s =
Lwt.return Lwt.return
(if String.length s = 36 then (if String.length s = 36 then
@ -99,9 +106,9 @@ let main_binary_of_uuid uuid db =
|> if_error "Error getting job build" |> if_error "Error getting job build"
~log:(fun e -> Log.warn (fun m -> m "Error getting job build: %a" pp_error e)) ~log:(fun e -> Log.warn (fun m -> m "Error getting job build: %a" pp_error e))
>>= fun (_id, build) -> >>= fun (_id, build) ->
match build.Builder_db.Build.main_binary with Model.not_found build.Builder_db.Build.main_binary
| None -> Lwt_result.fail ("Resource not found", `Not_Found) |> not_found_error
| Some main_binary -> >>= fun main_binary ->
Model.build_artifact_by_id main_binary db Model.build_artifact_by_id main_binary db
|> if_error "Error getting main binary" |> if_error "Error getting main binary"
@ -203,9 +210,8 @@ module Viz_aux = struct
artifacts artifacts
in in
begin begin
match debug_binary with Model.not_found debug_binary
| None -> Lwt_result.fail ("Error getting debug-binary", `Not_Found) |> not_found_error >>= fun debug_binary ->
| Some debug_binary ->
debug_binary.sha256 debug_binary.sha256
|> hex |> hex
|> Lwt_result.return |> Lwt_result.return
@ -216,9 +222,8 @@ module Viz_aux = struct
(fun p -> Fpath.(equal (v "opam-switch") (base p.localpath))) (fun p -> Fpath.(equal (v "opam-switch") (base p.localpath)))
artifacts artifacts
in in
match opam_switch with Model.not_found opam_switch
| None -> Lwt_result.fail ("Error getting opam-switch", `Not_Found) |> not_found_error >>= fun opam_switch ->
| Some opam_switch ->
opam_switch.sha256 opam_switch.sha256
|> hex |> hex
|> Lwt_result.return |> Lwt_result.return

View file

@ -15,7 +15,7 @@ let pp_error ppf = function
Caqti_error.pp ppf e Caqti_error.pp ppf e
let not_found = function let not_found = function
| None -> Lwt.return (Error `Not_found :> (_, [> error ]) result) | None -> Lwt_result.fail `Not_found
| Some v -> Lwt_result.return v | Some v -> Lwt_result.return v
let staging datadir = Fpath.(datadir / "_staging") let staging datadir = Fpath.(datadir / "_staging")

View file

@ -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 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 val staging : Fpath.t -> Fpath.t