123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281(* Copyright (C) 2017--2022 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 LGPL-3.0 Linking 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
* and the LGPL-3.0 Linking Exception along with this library. If not, see
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
*)(* Error Cause *)typeintegrity_constraint_violation=[|`Restrict_violation|`Not_null_violation|`Foreign_key_violation|`Unique_violation|`Check_violation|`Exclusion_violation|`Integrity_constraint_violation__don't_match]typeinsufficient_resources=[|`Disk_full|`Out_of_memory|`Too_many_connections|`Configuration_limit_exceeded|`Insufficient_resources__don't_match]typecause=[|integrity_constraint_violation|insufficient_resources|`Unspecified__don't_match]letshow_cause=function|`Restrict_violation->"RESTRICT violation"|`Not_null_violation->"NOT NULL constraint violation"|`Foreign_key_violation->"FOREIGN KEY constraint violation"|`Unique_violation->"UNIQUE constraint violation"|`Check_violation->"CHECK constraint violation"|`Exclusion_violation->"exclusion violation"|`Integrity_constraint_violation__don't_match->"integrity constraint violation"|`Disk_full->"disk full"|`Out_of_memory->"out of memory"|`Too_many_connections->"too many connections"|`Configuration_limit_exceeded->"configuration limit exceeded"|`Insufficient_resources__don't_match->"insufficient resources"|`Unspecified__don't_match->"unknown cause"(* Driver *)typemsg=..typemsg_impl={msg_pp:Format.formatter->msg->unit;msg_cause:msg->cause;}letmsg_impl=Hashtbl.create7letdefault_cause_=`Unspecified__don't_matchletdefine_msg~pp?(cause=default_cause)ec=Hashtbl.addmsg_implec{msg_pp=pp;msg_cause=cause}letfind_implmsg=letc=Obj.Extension_constructor.of_valmsgintryHashtbl.findmsg_implcwithNot_found->Printf.ksprintffailwith"Missing call to Caqti_error.define_msg for (%s _ : Caqti_error.msg)]"(Obj.Extension_constructor.namec)typemsg+=Msg:string->msgletis_punct=function|'.'|'!'|'?'->true|_->falselet()=letppppf=function|Msgs->Format.pp_print_stringppfs;ifs<>""&¬(is_puncts.[String.lengths-1])thenFormat.pp_print_charppf'.'|_->assertfalseindefine_msg~pp~cause:default_cause[%extension_constructorMsg]letpp_msgppfmsg=(find_implmsg).msg_ppppfmsg(* 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_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_failedofquery_error|`Response_rejectedofquery_error]typeretrieve=[`Decode_rejectedofcoding_error|`Request_failedofquery_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_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_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>"errletshow_of_pppperr=letbuf=Buffer.create128inletppf=Format.formatter_of_bufferbufinppppferr;Format.pp_print_flushppf();Buffer.contentsbufletshowerr=show_of_pppperrletcause=function|`Request_failederr|`Response_failederr->(find_impl(err:query_error).msg).msg_causeerr.msgtypecounit=|[@@@warning"-56"]letuncongested=function|Error#t|Ok_asx->x|Error(`Congested(nothingness:counit))->(matchnothingnesswith_->.)[@@@warning"+56"]exceptionExnoftlet()=Printexc.register_printer@@function|Exnerr->Some(showerr)|Caqti_query.Expand_errorerr->Some(show_of_ppCaqti_query.pp_expand_errorerr)|_->None