123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110(** Error types and exception handling for GPX operations *)(** Main error type *)typet=|Invalid_xmlofstring|Invalid_coordinateofstring|Missing_required_attributeofstring*string|Missing_required_elementofstring|Validation_errorofstring|Xml_errorofstring|IO_errorofstring(** GPX exception *)exceptionGpx_erroroft(** Result type for operations that can fail *)type'aresult=('a,t)Result.t(** {2 Error Operations} *)(** Convert error to string *)letto_string=function|Invalid_xmlmsg->"Invalid XML: "^msg|Invalid_coordinatemsg->"Invalid coordinate: "^msg|Missing_required_attribute(element,attr)->Printf.sprintf"Missing required attribute '%s' in element '%s'"attrelement|Missing_required_elementelement->Printf.sprintf"Missing required element '%s'"element|Validation_errormsg->"Validation error: "^msg|Xml_errormsg->"XML error: "^msg|IO_errormsg->"IO error: "^msg(** Pretty print error *)letppppferror=Format.fprintfppf"%s"(to_stringerror)(** Create invalid XML error *)letinvalid_xmlmsg=Invalid_xmlmsg(** Create invalid coordinate error *)letinvalid_coordinatemsg=Invalid_coordinatemsg(** Create missing attribute error *)letmissing_attributeelementattr=Missing_required_attribute(element,attr)(** Create missing element error *)letmissing_elementelement=Missing_required_elementelement(** Create validation error *)letvalidation_errormsg=Validation_errormsg(** Create XML error *)letxml_errormsg=Xml_errormsg(** Create IO error *)letio_errormsg=IO_errormsg(** Compare errors *)letcomparee1e2=String.compare(to_stringe1)(to_stringe2)(** Test error equality *)letequale1e2=comparee1e2=0(** {2 Result Helpers} *)(** Convert exception to result *)letcatchfx=tryOk(fx)withGpx_errore->Errore(** Convert result to exception *)letget_exn=function|Okx->x|Errore->raise(Gpx_errore)(** Map over result *)letmapf=function|Okx->Ok(fx)|Errore->Errore(** Bind over result *)letbindresultf=matchresultwith|Okx->fx|Errore->Errore(** Convert string result to error result *)letfrom_string_result=function|Okx->Okx|Errormsg->Error(Invalid_xmlmsg)(** {2 Error Classification} *)(** Check if error is XML-related *)letis_xml_error=function|Invalid_xml_|Xml_error_->true|_->false(** Check if error is coordinate-related *)letis_coordinate_error=function|Invalid_coordinate_->true|_->false(** Check if error is validation-related *)letis_validation_error=function|Validation_error_|Missing_required_attribute_|Missing_required_element_->true|_->false(** Check if error is IO-related *)letis_io_error=function|IO_error_->true|_->false