camlspotter avatar camlspotter committed 6eee1cc

better error handling

Comments (0)

Files changed (4)

 
 let ok200 = function
   | 200, v -> `Ok v
-  | n, mes -> `Error (`Http (n, mes))
+  | n, mes -> `Error (`Http (n, `Message mes))
 
 let wrap f =
   try f () with
     h#cleanup; (* Need to flush out cookies *)
     match code with
     | 200 -> Unix.rename tmp dst; `Ok dst
-    | e -> `Error (`Http (e, tmp))
+    | e -> `Error (`Http (e, `Downloaded tmp))
+
+module Error = struct
+  open Format
+
+  type t = [ `Http of int * [ `Message of string | `Downloaded of string ]
+           | `Curl of int * string ]
+
+  let format ppf = function
+    | `Http (n, _err) -> fprintf ppf "HTTP Error %d@." n
+    | `Curl (n, err) -> fprintf ppf "Curl Error %d: %s@." n err
+
+end
-val ok200 : int * 'a -> [> `Ok of 'a | `Error of [> `Http of int * 'a ]]
+open Spotlib.Spot
+
+val ok200 : int * string -> [> (string, [> `Http of int * [> `Message of string ]]) Result.t ]
 (** HTTP result to a result monad *)
 
 val get_string : 
   (Curl.handle -> unit) 
-  -> [> `Error of [> `Http of int * string 
-                  |  `Curl of int * string ]
-     | `Ok of string ]
+  -> [> (string, [> `Http of int * [> `Message of string ]
+                 |  `Curl of int * string ]) Result.t ]
 (** Get string *)
 
+module Error : sig
+  type t = [ `Curl of int * string  (** Curl library error *)
+           | `Http of int * [ `Message of string | `Downloaded of string ]  (** HTTP return status is not 200 *)
+           ]
+  val format : Format.t -> [< t ] -> unit
+end
 val download : 
   string 
   -> (Curl.handle -> unit) 
-  -> [> `Error of [> `Http of int * string 
-                  |  `Curl of int * string ]
-     | `Ok of string ]
+  -> [> (string, [> Error.t] ) Result.t ]
 (** Download file. Return the final downloaded file path.
     At error, the tmp file is not removed since it may contain error message. 
 *)
+
 module Error = struct
   open Format
 
+  type t = [ `Json_parse of exn
+           | `Json_conv of Json.t Meta_conv.Error.t
+           | Xcurl.Error.t
+           ]
+
   let wrap_json_conv (convf : 'a Json_conv.decoder) s = match convf s with
     | `Ok v -> `Ok v
     | `Error e -> `Error (`Json_conv e)
 
   let format ppf = function
-    | `Http (n, _err) -> fprintf ppf "HTTP Error %d@." n
-    | `Curl (n, _err) -> fprintf ppf "Curl Error %d@." n
     | `Json_parse exn -> fprintf ppf "Error at Json parse: %s@." (Printexc.to_string exn)
-    | `Other exn      -> fprintf ppf "Error: %s@." (Printexc.to_string exn)
-    | `Json_conv e -> Json_conv.format_full_error Format.stderr e 
+    | `Json_conv e -> Json_conv.format_full_error ppf e 
+    | #Xcurl.Error.t as e -> Xcurl.Error.format ppf e
 
-  let from_Ok = function
-    | `Ok v -> v
-    | `Error e -> failwith (Format.sprintf "%a" format e)
 end
 
 let curl_get_and_parse (convf : 'a Json_conv.decoder) curlf = 
 val parse : string -> [> `Error of [> `Json_parse of exn ] | `Ok of Json.t ]
 
 module Error : sig
+  type t =
+    [ `Json_conv of Json.t Meta_conv.Error.t (** conversion error from JSON to OCaml value *)
+    | `Json_parse of exn (** parser error of JSON *)
+    | Xcurl.Error.t
+    ]
 
-  val wrap_json_conv : 
+  val format : Format.t -> [< t ] -> unit
+
+  val wrap_json_conv :
     'a Json_conv.decoder 
-    -> Json.t 
-    ->  ('a, [> `Json_conv of Json.t Meta_conv.Error.t]) Result.t
-
-  val format : Format.t 
-    -> [< `Http of int * 'a
-       |  `Curl of int * string
-       |  `Json_conv of Json.t Meta_conv.Error.t
-       |  `Json_parse of exn
-       |  `Other of exn ] 
-    -> unit
-
-  val from_Ok :
-    [< `Error of [< `Http of int * 'a
-                 |  `Curl of int * string
-                 |  `Json_conv of Json.t Meta_conv.Error.t
-                 |  `Json_parse of exn
-                 |  `Other of exn ]
-    | `Ok of 'b ] 
-    -> 'b
+    -> Json.t
+    -> [> ('a, [> `Json_conv of Json.t Meta_conv.Error.t ]) Result.t ]
 end
 
 val curl_get_and_parse :
   'a Json_conv.decoder 
   -> (Curl.handle -> unit) 
-  -> ('a, [> `Http of int * string
+  -> ('a, [> `Http of int * [> `Message of string ]
           |  `Curl of int * string
           |  `Json_conv of Json.t Meta_conv.Error.t
           |  `Json_parse of exn ]) Result.t
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.