123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211(* Copyright (C) 2017--2019 Petter A. Urkedal <paurkedal@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version, with the OCaml static compilation exception.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*)openCaqti_compat[@@warning"-33"](* Driver *)typemsg=..letmsg_pp=Hashtbl.create7letdefine_msg~ppec=Hashtbl.addmsg_ppecppletpp_msgppfmsg=letc=Obj.Extension_constructor.of_valmsgintryletpp=Hashtbl.findmsg_ppcinppppfmsgwithNot_found->Format.fprintfppf"[FIXME: missing printer for (%s _ : Caqti_error.msg)]"(Obj.Extension_constructor.namec)typemsg+=Msg:string->msglet()=letppppf=function|Msgs->Format.pp_print_stringppfs|_->assertfalseindefine_msg~pp[%extension_constructorMsg](* We don't want to expose any DB password in error messages. *)letpp_urippfuri=(matchUri.passworduriwith|None->Uri.pp_humppfuri|Some_->Uri.pp_humppf(Uri.with_passworduri(Some"_")))(* Records *)typeload_error={uri:Uri.t;msg:msg;}letpp_load_msgppffmterr=Format.fprintfppffmtpp_urierr.uri;Format.pp_print_stringppf": ";pp_msgppferr.msgtypeconnection_error={uri:Uri.t;msg:msg;}letpp_connection_msgppffmterr=Format.fprintfppffmtpp_urierr.uri;Format.pp_print_stringppf": ";pp_msgppferr.msgtypequery_error={uri:Uri.t;query:string;msg:msg;}letpp_query_msgppffmterr=Format.fprintfppffmtpp_urierr.uri;Format.pp_print_stringppf": ";pp_msgppferr.msg;Format.fprintfppf" Query: %S."err.querytypecoding_error={uri:Uri.t;typ:Caqti_type.any;msg:msg;}letpp_coding_errorppffmterr=Format.fprintfppffmtCaqti_type.pp_anyerr.typpp_urierr.uri;Format.pp_print_stringppf": ";pp_msgppferr.msg(* Load *)letload_rejected~urimsg=`Load_rejected({uri;msg}:load_error)letload_failed~urimsg=`Load_failed({uri;msg}:load_error)(* Connect *)letconnect_rejected~urimsg=`Connect_rejected({uri;msg}:connection_error)letconnect_failed~urimsg=`Connect_failed({uri;msg}:connection_error)(* Call *)letencode_missing~uri~field_type()=lettyp=Caqti_type.Any(Caqti_type.fieldfield_type)inletmsg=Msg"Field type not supported and no fallback provided."in`Encode_rejected({uri;typ;msg}:coding_error)letencode_rejected~uri~typmsg=lettyp=Caqti_type.Anytypin`Encode_rejected({uri;typ;msg}:coding_error)letencode_failed~uri~typmsg=lettyp=Caqti_type.Anytypin`Encode_failed({uri;typ;msg}:coding_error)letrequest_rejected~uri~querymsg=`Request_rejected({uri;query;msg}:query_error)letrequest_failed~uri~querymsg=`Request_failed({uri;query;msg}:query_error)(* Retrieve *)letdecode_missing~uri~field_type()=lettyp=Caqti_type.Any(Caqti_type.fieldfield_type)inletmsg=Msg"Field type not supported and no fallback provided."in`Decode_rejected({uri;typ;msg}:coding_error)letdecode_rejected~uri~typmsg=lettyp=Caqti_type.Anytypin`Decode_rejected({uri;typ;msg}:coding_error)letresponse_failed~uri~querymsg=`Response_failed({uri;query;msg}:query_error)letresponse_rejected~uri~querymsg=`Response_rejected({uri;query;msg}:query_error)(* Common *)typecall=[`Encode_rejectedofcoding_error|`Encode_failedofcoding_error|`Request_rejectedofquery_error|`Request_failedofquery_error|`Response_rejectedofquery_error]typeretrieve=[`Decode_rejectedofcoding_error|`Response_failedofquery_error|`Response_rejectedofquery_error]typecall_or_retrieve=[call|retrieve]typetransact=call_or_retrievetypeload=[`Load_rejectedofload_error|`Load_failedofload_error]typeconnect=[`Connect_rejectedofconnection_error|`Connect_failedofconnection_error|`Post_connectofcall_or_retrieve]typeload_or_connect=[load|connect]typet=[load|connect|call|retrieve]letrecuri:'a.([<t]as'a)->Uri.t=function|`Load_rejected({uri;_}:load_error)->uri|`Load_failed({uri;_}:load_error)->uri|`Connect_rejected({uri;_}:connection_error)->uri|`Connect_failed({uri;_}:connection_error)->uri|`Post_connecterr->urierr|`Encode_rejected({uri;_}:coding_error)->uri|`Encode_failed({uri;_}:coding_error)->uri|`Request_rejected({uri;_}:query_error)->uri|`Request_failed({uri;_}:query_error)->uri|`Decode_rejected({uri;_}:coding_error)->uri|`Response_failed({uri;_}:query_error)->uri|`Response_rejected({uri;_}:query_error)->uriletrecpp:'a._->([<t]as'a)->unit=funppf->function|`Load_rejectederr->pp_load_msgppf"Cannot load driver for <%a>"err|`Load_failederr->pp_load_msgppf"Failed to load driver for <%a>"err|`Connect_rejectederr->pp_connection_msgppf"Cannot connect to <%a>"err|`Connect_failederr->pp_connection_msgppf"Failed to connect to <%a>"err|`Post_connecterr->Format.pp_print_stringppf"During post-connect: ";ppppferr|`Encode_rejectederr->pp_coding_errorppf"Cannot encode %a for <%a>"err|`Encode_failederr->pp_coding_errorppf"Failed to bind %a for <%a>"err|`Decode_rejectederr->pp_coding_errorppf"Cannot decode %a from <%a>"err|`Request_rejectederr->pp_query_msgppf"Request rejected by <%a>"err|`Request_failederr->pp_query_msgppf"Request to <%a> failed"err|`Response_failederr->pp_query_msgppf"Response from <%a> failed"err|`Response_rejectederr->pp_query_msgppf"Unexpected result from <%a>"errletshowerr=letbuf=Buffer.create128inletppf=Format.formatter_of_bufferbufinppppferr;Format.pp_print_flushppf();Buffer.contentsbufletuncongested=function|Error#t|Ok_asx->x|Error`Congestedx->Caqti_common.absurdxexceptionExnoftlet()=Printexc.register_printer(functionExnerr->Some(showerr)|_->None)