refactor: nicer handling of errors and warning log for recoverable errors
This commit is contained in:
parent
c6eb1af82c
commit
1b3af2ac91
|
@ -450,10 +450,14 @@ execute_sql_in_clickhouse_server_using_connection(Connection, SQL) ->
|
||||||
|
|
||||||
%% This function transforms the result received from clickhouse to something
|
%% This function transforms the result received from clickhouse to something
|
||||||
%% that is a little bit more readable and creates approprieate log messages
|
%% that is a little bit more readable and creates approprieate log messages
|
||||||
transform_and_log_clickhouse_result({ok, 200, <<"">>} = _ClickhouseResult, _, _) ->
|
transform_and_log_clickhouse_result({ok, ResponseCode, <<"">>} = _ClickhouseResult, _, _) when
|
||||||
|
ResponseCode =:= 200; ResponseCode =:= 204
|
||||||
|
->
|
||||||
snabbkaffe_log_return(ok),
|
snabbkaffe_log_return(ok),
|
||||||
ok;
|
ok;
|
||||||
transform_and_log_clickhouse_result({ok, 200, Data}, _, _) ->
|
transform_and_log_clickhouse_result({ok, ResponseCode, Data}, _, _) when
|
||||||
|
ResponseCode =:= 200; ResponseCode =:= 204
|
||||||
|
->
|
||||||
Result = {ok, Data},
|
Result = {ok, Data},
|
||||||
snabbkaffe_log_return(Result),
|
snabbkaffe_log_return(Result),
|
||||||
Result;
|
Result;
|
||||||
|
@ -464,24 +468,58 @@ transform_and_log_clickhouse_result(ClickhouseErrorResult, ResourceID, SQL) ->
|
||||||
sql => SQL,
|
sql => SQL,
|
||||||
reason => ClickhouseErrorResult
|
reason => ClickhouseErrorResult
|
||||||
}),
|
}),
|
||||||
case ClickhouseErrorResult of
|
case is_recoverable_error(ClickhouseErrorResult) of
|
||||||
%% TODO: The hackeny errors that the clickhouse library forwards are
|
%% TODO: The hackeny errors that the clickhouse library forwards are
|
||||||
%% very loosely defined. We should try to make sure that the following
|
%% very loosely defined. We should try to make sure that the following
|
||||||
%% handles all error cases that we need to handle as recoverable_error
|
%% handles all error cases that we need to handle as recoverable_error
|
||||||
{error, ecpool_empty} ->
|
true ->
|
||||||
{error, {recoverable_error, ecpool_empty}};
|
?SLOG(warning, #{
|
||||||
{error, econnrefused} ->
|
msg => "clickhouse connector: sql query failed (recoverable)",
|
||||||
{error, {recoverable_error, econnrefused}};
|
recoverable_error => true,
|
||||||
{error, closed} ->
|
connector => ResourceID,
|
||||||
{error, {recoverable_error, closed}};
|
sql => SQL,
|
||||||
{error, {closed, PartialBody}} ->
|
reason => ClickhouseErrorResult
|
||||||
{error, {recoverable_error, {closed_partial_body, PartialBody}}};
|
}),
|
||||||
{error, disconnected} ->
|
to_recoverable_error(ClickhouseErrorResult);
|
||||||
{error, {recoverable_error, disconnected}};
|
false ->
|
||||||
_ ->
|
?SLOG(error, #{
|
||||||
{error, ClickhouseErrorResult}
|
msg => "clickhouse connector: sql query failed (unrecoverable)",
|
||||||
|
recoverable_error => false,
|
||||||
|
connector => ResourceID,
|
||||||
|
sql => SQL,
|
||||||
|
reason => ClickhouseErrorResult
|
||||||
|
}),
|
||||||
|
to_error_tuple(ClickhouseErrorResult)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
to_recoverable_error({error, Reason}) ->
|
||||||
|
{error, {recoverable_error, Reason}};
|
||||||
|
to_recoverable_error(Error) ->
|
||||||
|
{error, {recoverable_error, Error}}.
|
||||||
|
|
||||||
|
to_error_tuple({error, Reason}) ->
|
||||||
|
{error, {unrecoverable_error, Reason}};
|
||||||
|
to_error_tuple(Error) ->
|
||||||
|
{error, {unrecoverable_error, Error}}.
|
||||||
|
|
||||||
|
is_recoverable_error({error, Reason}) ->
|
||||||
|
is_recoverable_error_reason(Reason);
|
||||||
|
is_recoverable_error(_) ->
|
||||||
|
false.
|
||||||
|
|
||||||
|
is_recoverable_error_reason(ecpool_empty) ->
|
||||||
|
true;
|
||||||
|
is_recoverable_error_reason(econnrefused) ->
|
||||||
|
true;
|
||||||
|
is_recoverable_error_reason(closed) ->
|
||||||
|
true;
|
||||||
|
is_recoverable_error_reason({closed, _PartialBody}) ->
|
||||||
|
true;
|
||||||
|
is_recoverable_error_reason(disconnected) ->
|
||||||
|
true;
|
||||||
|
is_recoverable_error_reason(_) ->
|
||||||
|
false.
|
||||||
|
|
||||||
snabbkaffe_log_return(_Result) ->
|
snabbkaffe_log_return(_Result) ->
|
||||||
?tp(
|
?tp(
|
||||||
clickhouse_connector_query_return,
|
clickhouse_connector_query_return,
|
||||||
|
|
Loading…
Reference in New Issue