GPS Exchange Format library/CLI in OCaml
at main 3.0 kB view raw
1(** Error types and exception handling for GPX operations *) 2 3(** Main error type *) 4type t = 5 | Invalid_xml of string 6 | Invalid_coordinate of string 7 | Missing_required_attribute of string * string 8 | Missing_required_element of string 9 | Validation_error of string 10 | Xml_error of string 11 | IO_error of string 12 13(** GPX exception *) 14exception Gpx_error of t 15 16(** Result type for operations that can fail *) 17type 'a result = ('a, t) Result.t 18 19(** {2 Error Operations} *) 20 21(** Convert error to string *) 22let to_string = function 23 | Invalid_xml msg -> "Invalid XML: " ^ msg 24 | Invalid_coordinate msg -> "Invalid coordinate: " ^ msg 25 | Missing_required_attribute (element, attr) -> 26 Printf.sprintf "Missing required attribute '%s' in element '%s'" attr element 27 | Missing_required_element element -> 28 Printf.sprintf "Missing required element '%s'" element 29 | Validation_error msg -> "Validation error: " ^ msg 30 | Xml_error msg -> "XML error: " ^ msg 31 | IO_error msg -> "IO error: " ^ msg 32 33(** Pretty print error *) 34let pp ppf error = Format.fprintf ppf "%s" (to_string error) 35 36(** Create invalid XML error *) 37let invalid_xml msg = Invalid_xml msg 38 39(** Create invalid coordinate error *) 40let invalid_coordinate msg = Invalid_coordinate msg 41 42(** Create missing attribute error *) 43let missing_attribute element attr = Missing_required_attribute (element, attr) 44 45(** Create missing element error *) 46let missing_element element = Missing_required_element element 47 48(** Create validation error *) 49let validation_error msg = Validation_error msg 50 51(** Create XML error *) 52let xml_error msg = Xml_error msg 53 54(** Create IO error *) 55let io_error msg = IO_error msg 56 57(** Compare errors *) 58let compare e1 e2 = String.compare (to_string e1) (to_string e2) 59 60(** Test error equality *) 61let equal e1 e2 = compare e1 e2 = 0 62 63(** {2 Result Helpers} *) 64 65(** Convert exception to result *) 66let catch f x = 67 try Ok (f x) 68 with Gpx_error e -> Error e 69 70(** Convert result to exception *) 71let get_exn = function 72 | Ok x -> x 73 | Error e -> raise (Gpx_error e) 74 75(** Map over result *) 76let map f = function 77 | Ok x -> Ok (f x) 78 | Error e -> Error e 79 80(** Bind over result *) 81let bind result f = 82 match result with 83 | Ok x -> f x 84 | Error e -> Error e 85 86(** Convert string result to error result *) 87let from_string_result = function 88 | Ok x -> Ok x 89 | Error msg -> Error (Invalid_xml msg) 90 91(** {2 Error Classification} *) 92 93(** Check if error is XML-related *) 94let is_xml_error = function 95 | Invalid_xml _ | Xml_error _ -> true 96 | _ -> false 97 98(** Check if error is coordinate-related *) 99let is_coordinate_error = function 100 | Invalid_coordinate _ -> true 101 | _ -> false 102 103(** Check if error is validation-related *) 104let is_validation_error = function 105 | Validation_error _ | Missing_required_attribute _ | Missing_required_element _ -> true 106 | _ -> false 107 108(** Check if error is IO-related *) 109let is_io_error = function 110 | IO_error _ -> true 111 | _ -> false