123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)typerpc_error=|Empty_answer|Connection_failedofstring|Bad_requestofstring|Forbidden|Method_not_allowedofTezos_rpc.Service.methlist|Unsupported_media_typeofstringoption|Not_acceptableof{proposed:string;acceptable:string}|Unexpected_status_codeof{code:Cohttp.Code.status_code;content:string;media_type:stringoption;}|Unexpected_content_typeof{received:string;acceptable:stringlist;body:string;}|Unexpected_contentof{content:string;media_type:string;error:string;}|OCaml_exceptionofstring|Unauthorized_hostofstringoption|Unauthorized_uri|Too_many_redirectsofstring|Redirect_without_locationofstringtypeerror+=|Request_failedof{meth:Tezos_rpc.Service.meth;uri:Uri.t;error:rpc_error;}letrpc_error_encoding=letopenData_encodinginunion[case(Tag0)~title:"Empty_answer"(obj1(req"kind"(constant"empty_answer")))(functionEmpty_answer->Some()|_->None)(fun()->Empty_answer);case(Tag1)~title:"Connection_failed"(obj2(req"kind"(constant"connection_failed"))(req"message"string))(functionConnection_failedmsg->Some((),msg)|_->None)(function(),msg->Connection_failedmsg);case(Tag2)~title:"Bad_request"(obj2(req"kind"(constant"bad_request"))(req"message"string))(functionBad_requestmsg->Some((),msg)|_->None)(function(),msg->Bad_requestmsg);case(Tag3)~title:"Method_not_allowed"(obj2(req"kind"(constant"method_not_allowed"))(req"allowed"(listTezos_rpc.Service.meth_encoding)))(functionMethod_not_allowedmeths->Some((),meths)|_->None)(function(),meths->Method_not_allowedmeths);case(Tag4)~title:"Unsupported_media_type"(obj2(req"kind"(constant"unsupported_media_type"))(opt"content_type"string))(functionUnsupported_media_typem->Some((),m)|_->None)(function(),m->Unsupported_media_typem);case(Tag5)~title:"Not_acceptable"(obj3(req"kind"(constant"not_acceptable"))(req"proposed"string)(req"acceptable"string))(function|Not_acceptable{proposed;acceptable}->Some((),proposed,acceptable)|_->None)(function|(),proposed,acceptable->Not_acceptable{proposed;acceptable});case(Tag6)~title:"Unexpected_status_code"(obj4(req"kind"(constant"unexpected_status_code"))(req"code"uint16)(req"content"string)(opt"media_type"string))(function|Unexpected_status_code{code;content;media_type}->Some((),Cohttp.Code.code_of_statuscode,content,media_type)|_->None)(function|(),code,content,media_type->letcode=Cohttp.Code.status_of_codecodeinUnexpected_status_code{code;content;media_type});case(Tag7)~title:"Unexpected_content_type"(obj4(req"kind"(constant"unexpected_content_type"))(req"received"string)(req"acceptable"(liststring))(req"body"string))(function|Unexpected_content_type{received;acceptable;body}->Some((),received,acceptable,body)|_->None)(function|(),received,acceptable,body->Unexpected_content_type{received;acceptable;body});case(Tag8)~title:"Unexpected_content"(obj4(req"kind"(constant"unexpected_content"))(req"content"string)(req"media_type"string)(req"error"string))(function|Unexpected_content{content;media_type;error}->Some((),content,media_type,error)|_->None)(function|(),content,media_type,error->Unexpected_content{content;media_type;error});case(Tag9)~title:"OCaml_exception"(obj2(req"kind"(constant"ocaml_exception"))(req"content"string))(functionOCaml_exceptionmsg->Some((),msg)|_->None)(function(),msg->OCaml_exceptionmsg);case(Tag10)~title:"Unauthorized URI"unit(functionUnauthorized_uri->Some()|_->None)(function()->Unauthorized_uri);case(Tag11)~title:"Too many redirects"(obj2(req"kind"(constant"too_many_redirects"))(req"content"string))(functionToo_many_redirectsmsg->Some((),msg)|_->None)(function(),msg->Too_many_redirectsmsg);case(Tag12)~title:"Redirect without location"(obj2(req"kind"(constant"redirect_without_location"))(req"content"string))(functionRedirect_without_locationmsg->Some((),msg)|_->None)(function(),msg->Redirect_without_locationmsg);]letpp_rpc_errorppferr=matcherrwith|Empty_answer->Format.fprintfppf"The server answered with an empty response."|Connection_failedmsg->Format.fprintfppf"Unable to connect to the node: \"%s\""msg|Bad_requestmsg->Format.fprintfppf"@[<v 2>Oops! It looks like we forged an invalid HTTP request.@,%s@]"msg|Forbidden->Format.fprintfppf"@[<v 2>The server forbids access.@]"|Method_not_allowedmeths->Format.fprintfppf"@[<v 2>The requested service only accepts the following method:@ %a@]"(Format.pp_print_list(funppfm->Format.pp_print_stringppf(Tezos_rpc.Service.string_of_methm)))meths|Unsupported_media_typeNone->Format.fprintfppf"@[<v 2>The server wants to known the media type we used.@]"|Unsupported_media_type(Somemedia)->Format.fprintfppf"@[<v 2>The server does not support the media type we used: %s.@]"media|Not_acceptable{proposed;acceptable}->Format.fprintfppf"@[<v 2>No intersection between the media types we accept and the \
ones the server is able to send.@,\
\ We proposed: %s@,\
\ The server is only able to serve: %s."proposedacceptable|Unexpected_status_code{code;content;_}->Format.fprintfppf"@[<v 2>Unexpected error %d:@,%S"(Cohttp.Code.code_of_statuscode)content|Unexpected_content_type{received;acceptable=_;body}->Format.fprintfppf"@[<v 0>The server answered with a media type we do not understand: \
%s.@,\
The response body was:@,\
%s@]"receivedbody|Unexpected_content{content;media_type;error}->Format.fprintfppf"@[<v 2>Failed to parse the answer (%s):@,\
@[<v 2>error:@ %s@]@,\
@[<v 2>content:@ %S@]@]"media_typeerrorcontent|OCaml_exceptionmsg->Format.fprintfppf"@[<v 2>The server failed with an unexpected exception:@ %s@]"msg|Unauthorized_hosthost->Format.fprintfppf"@[<v 2>The server refused connection to host \"%s\", please check the \
node settings for CORS allowed origins.@]"(Option.value~default:""host)|Unauthorized_uri->Format.fprintfppf"@[<v 2>The server doesn't authorize this endpoint (ACL filtering).@]"|Too_many_redirectsmsg->Format.fprintfppf"@[<v 2>Too many redirects: %s.@]"msg|Redirect_without_locationmsg->Format.fprintfppf"@[<v 2>Redirect without location: %s.@]"msglet()=register_error_kind`Permanent~id:"rpc_client.request_failed"~title:""~description:""~pp:(funppf(meth,uri,error)->Format.fprintfppf"@[<v 2>Rpc request failed:@ - meth: %s@ - uri: %s@ - error: %a@]"(Tezos_rpc.Service.string_of_methmeth)(Uri.to_stringuri)pp_rpc_errorerror)Data_encoding.(obj3(req"meth"Tezos_rpc.Service.meth_encoding)(req"uri"Tezos_rpc.Encoding.uri_encoding)(req"error"rpc_error_encoding))(function|Request_failed{uri;error;meth}->Some(meth,uri,error)|_->None)(fun(meth,uri,error)->Request_failed{uri;meth;error})