From cde2a7ed552fb7e8528635c93d590e922d43028f Mon Sep 17 00:00:00 2001 From: Maciej Szlosarczyk Date: Fri, 7 Jun 2019 15:38:11 +0300 Subject: [PATCH] Handle case when registry is not reacheable --- apps/epp_proxy/src/epp_http_client.erl | 41 +++++++++++++++++++++----- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/apps/epp_proxy/src/epp_http_client.erl b/apps/epp_proxy/src/epp_http_client.erl index 40ac340..3f89350 100644 --- a/apps/epp_proxy/src/epp_http_client.erl +++ b/apps/epp_proxy/src/epp_http_client.erl @@ -9,17 +9,23 @@ %% Callback API request(#epp_request{} = Request) -> HackneyArgs = handle_args(Request), - {Status, _StatusCode, _Headers, ClientRef} = - apply(hackney, request, HackneyArgs), - {ok, Body} = hackney:body(ClientRef), - {Status, Body}. + case apply(hackney, request, HackneyArgs) of + {error, Error} -> + log_and_return_canned(Error, Request); + {Status, _StatusCode, _Headers, ClientRef} -> + {ok, Body} = hackney:body(ClientRef), + {Status, Body} + end. error_request(#epp_error_request{} = Request) -> HackneyArgs = handle_error_args(Request), - {Status, _StatusCode, _Headers, ClientRef} = - apply(hackney, request, HackneyArgs), - {ok, Body} = hackney:body(ClientRef), - {Status, Body}. + case apply(hackney, request, HackneyArgs) of + {error, Error} -> + log_and_return_canned(Error, Request); + {Status, _StatusCode, _Headers, ClientRef} -> + {ok, Body} = hackney:body(ClientRef), + {Status, Body} + end. request_builder(Map) -> request_from_map(Map). @@ -102,3 +108,22 @@ query_params(Code, Message, nomatch) -> [{<<"code">>, Code}, {<<"msg">>, Message}]; query_params(Code, Message, ClTRID) -> [{<<"code">>, Code}, {<<"msg">>, Message}, {<<"clTRID">>, ClTRID}]. + +%% Log critical information about a request that failed, and then +%% return a canned response with internal error status. +log_and_return_canned(Error, Request) -> + lager:alert("Registry cannot be reached!"), + lager:alert("Error contacting registry: [~p, ~p]~n", [Error, Request]), + {2400, canned_response()}. + +%% In case registry is not accessible, return this response. +%% In the future, this should be configurable. +canned_response() -> + <<" + + + + Internal server error. + + +">>.