From c51af554e714a5e8ab1666cd355cb203e5557bf4 Mon Sep 17 00:00:00 2001 From: ndiezel0 Date: Tue, 25 Feb 2020 18:38:58 +0300 Subject: [PATCH] CDS-101: Add filter for stacktrace (#20) --- apps/kds/src/kds_thrift_handler_utils.erl | 46 ++++++++++++++++++----- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/apps/kds/src/kds_thrift_handler_utils.erl b/apps/kds/src/kds_thrift_handler_utils.erl index 703f11f..e08d848 100644 --- a/apps/kds/src/kds_thrift_handler_utils.erl +++ b/apps/kds/src/kds_thrift_handler_utils.erl @@ -18,7 +18,7 @@ filter_fun_exceptions(Fun) -> error:{woody_error, _} = WoodyError:Stacktrace -> erlang:raise(error, WoodyError, Stacktrace); Class:Exception:Stacktrace -> - erlang:raise(Class, filter_error_reason(Exception), Stacktrace) + erlang:raise(Class, filter_error_reason(Exception), filter_stacktrace(Stacktrace)) end end. @@ -33,19 +33,45 @@ raise(Exception) -> % Known safe errors filter_error_reason({hash_collision_detected, _Hash} = Reason) -> Reason; -% Generic safe errors -filter_error_reason(Reason) when is_tuple(Reason) -> - erlang:list_to_tuple([filter_error_reason(R) || R <- erlang:tuple_to_list(Reason)]); -filter_error_reason(Reason) when is_list(Reason) -> - [filter_error_reason(R) || R <- Reason]; -filter_error_reason(Reason) when is_map(Reason) -> +filter_error_reason(Reason) -> + filter(Reason). + +filter_stacktrace(Stacktrace) when is_list(Stacktrace) -> + [filter_stacktrace_item(ST) || ST <- Stacktrace]. + +filter_stacktrace_item({Module, Function, Arity, Location} = ST) when + is_atom(Module) andalso + is_atom(Function) andalso + is_integer(Arity) andalso + is_list(Location) +-> + ST; +filter_stacktrace_item({Module, Function, Args, Location}) when + is_atom(Module) andalso + is_atom(Function) andalso + is_list(Args) andalso + is_list(Location) +-> + {Module, Function, filter_stacktrace_args(Args), Location}; +filter_stacktrace_item(_) -> + '***'. + +filter_stacktrace_args(Args) -> + filter(Args). + +% Generic filter +filter(Reason) when is_tuple(Reason) -> + erlang:list_to_tuple([filter(R) || R <- erlang:tuple_to_list(Reason)]); +filter(Reason) when is_list(Reason) -> + [filter(R) || R <- Reason]; +filter(Reason) when is_map(Reason) -> maps:map( fun(_Key, Value) -> - filter_error_reason(Value) + filter(Value) end, Reason ); -filter_error_reason(Reason) when +filter(Reason) when is_atom(Reason) orelse is_number(Reason) orelse is_reference(Reason) orelse @@ -53,5 +79,5 @@ filter_error_reason(Reason) when -> Reason; % Other -filter_error_reason(_Reason) -> +filter(_Reason) -> '***'.