1. Yaron Minsky
  2. screentime-monitor

Commits

Yaron Minsky  committed a0a1699

further cleaned up error handling

  • Participants
  • Parent commits 2d5e673
  • Branches default

Comments (0)

Files changed (1)

File client.ml

View file
   Command.async_basic
     ~summary:"Monitor your time spent"
     (shared_flags ())
-    (fun user () -> setup_conn user (fun user conn ->
-       let stop = force on_term_signal in
+    (fun user () -> 
        force clear_string
        >>= fun cstring ->
        retry
          ~on_error:(fun err ->
            printf "%sFailed.  Will retry\n\n%s\n"
              cstring (Exn.sexp_of_t err |> Sexp.to_string_hum))
-         (fun () ->
-            Clock.every' (sec 1.) ~stop (fun () ->
-              let spinner = force spinner in
-              Rpc.Rpc.dispatch Protocol.status conn user
-              >>| function
-              | Error _ ->
-                printf "%s%s Unable to reach server\n" cstring (spinner ())
-              | Ok { Protocol.Status. state; elapsed } ->
-                let total_elapsed =
-                  Map.data elapsed
-                  |> List.fold ~init:Time.Span.zero ~f:Time.Span.(+)
-                in
-                printf "%s%s %s %s\n"
-                  cstring
-                  (spinner ())
-                  (span_string total_elapsed)
-                  (match state with
-                   | `Active cat -> sprintf "{%s}" (Category.to_string cat)
-                   | `Not_running -> "(paused)"
-                  ));
-            (stop >>| fun x -> Ok x)
-         )
-       |> Deferred.ignore
-     ))
+         (fun () -> try_with (fun () ->
+            setup_conn user (fun user conn ->
+              let stop = force on_term_signal in
+              Clock.every' (sec 1.) ~stop (fun () ->
+                let spinner = force spinner in
+                Rpc.Rpc.dispatch Protocol.status conn user
+                >>| function
+                | Error err ->
+                  printf "%s%s Unable to reach server\n\n%s\n"
+                    cstring
+                    (Error.sexp_of_t err |> Sexp.to_string_hum)
+                    (spinner ())
+                | Ok { Protocol.Status. state; elapsed } ->
+                  let total_elapsed =
+                    Map.data elapsed
+                    |> List.fold ~init:Time.Span.zero ~f:Time.Span.(+)
+                  in
+                  printf "%s%s %s %s\n"
+                    cstring
+                    (spinner ())
+                    (span_string total_elapsed)
+                    (match state with
+                     | `Active cat -> sprintf "{%s}" (Category.to_string cat)
+                     | `Not_running -> "(paused)"
+                    ));
+              stop
+            )))
+       >>| function
+       | Ok () -> ()
+       | Error err ->
+         printf "Failed.\n\n%s\n" (Exn.sexp_of_t err |> Sexp.to_string_hum)
+    )
+              
+            
 
 module Ascii_table = Textutils.Ascii_table