Commits

seanmcl committed 7ce1c38

catch all server exns

  • Participants
  • Parent commits 84288d9

Comments (0)

Files changed (3)

 bug fixes
 =========
 
+- server catches and logs all exceptions.  This is good in itself, and in
+  addition prevents a bug where text on stderr was eval'ed by emacs.
+
 --------------------------------------------------------------------------------
 
 features
 
    - tuareg    
    - ocamlspot 
+
+If the unix domain socket file is deleted is running, a 

File omake_server.ml

 module Server : sig
   val logf : ('a, unit, string, unit) format4 -> 'a
   val wait_for_log : unit -> unit deferred
+  val log_exn : exn -> unit
 end = struct
   let log_writer = lazy (Writer.open_file File.server_log)
   let wait_for_log () = Lazy.force log_writer >>| fun _ -> ()
       ksprintf (fun s -> printf "%s" (log_msg s)) fmt
     | Some writer ->
       ksprintf (fun s -> Writer.writef writer "%s" (log_msg s)) fmt
+  let log_exn e = logf "Caught exception: %s" (Exn.to_string e)
 end
 
 (* -------------------------------------------------------------------------- *)
           Server.logf
             "Can't read env file for %s (%s). Writing new file from bash env."
             (Id.to_string id)
-            (Error.to_string_hum error);
+            (Exn.to_string error);
           (* (Error.to_string_hum error); *)
           create id
       end
         Writer.close writer) >>= function
     | Ok () -> Deferred.unit
     | Error exn ->
-      Server.logf "Caught exception: %s" (Exn.to_string (Monitor.extract_exn exn));
+      Server.log_exn (Monitor.extract_exn exn);
       Deferred.unit)
 
 let connect msg =
   (* Make sure the message ends with a space so the sexp parser knows
      it's finished *)
   let msg = msg ^ " " in
-  Tcp.connect_unix ~file:File.socket () >>= fun (reader, writer) ->
-  Writer.write writer msg;
-  Pipe.iter (Reader.pipe reader) ~f:(fun s ->
-    printf "%s\n" s;
-    Deferred.unit)
+  let file = File.socket in
+  Asys.file_exists_exn file >>= function
+  | false -> failwithf "Missing socket file: %s" file ()
+  | true -> 
+    Tcp.connect_unix ~file () >>= fun (reader, writer) ->
+    Writer.write writer msg;
+    Pipe.iter (Reader.pipe reader) ~f:(fun s ->
+      printf "%s\n" s;
+      Deferred.unit)
 
 (* -------------------------------------------------------------------------- *)
 (*  Command                                                                   *)
   )
   (fun pid -> schedule
     ~f:(fun () ->
-      To_emacs.set_pid pid;
-      serve ())
+      try_with (fun () -> 
+        To_emacs.set_pid pid;
+        serve ()) >>| function
+      | Ok () -> ()
+      | Error exn -> Server.log_exn exn)
     ())
 
 let send_cmd =
     )
     (fun msg ->
       schedule
-      ~f:(fun () -> connect msg)
+      ~f:(fun () -> 
+        try_with (fun () -> connect msg) >>| function
+        | Error exn -> 
+          Server.log_exn exn;
+          To_emacs.send_async "(message \"Omake server error.  Do M-x Omake.show-server-log\")"
+        | Ok () -> ())
       ~quit:()
       ())
 
     (fun () ->
       schedule
         ~f:(fun () -> try_with (fun () -> connect "(Ping 0)") >>| function
-        | Ok _ -> shutdown 0
+        | Ok () -> shutdown 0
         | Error _ -> shutdown 1)
         ~quit:()
         ())
       let msg = sprintf "(Show_model %s)" id in
       schedule
         ~f:(fun () -> try_with (fun () -> connect msg) >>| function
-        | Ok _ -> shutdown 0
-        | Error _ -> shutdown 1)
+        | Ok () -> shutdown 0
+        | Error exn -> 
+          printf "Omake server error: %s\n" (Exn.to_string exn);
+          shutdown 1)
         ~quit:()
         ())