Allow to show safe errors (#10)

Port changes from rbkmoney/cds#92
This commit is contained in:
Andrey Fadeev 2019-08-26 15:50:59 +03:00 committed by GitHub
parent c022cceb1c
commit b2dd69cd30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 22 deletions

View File

@ -18,18 +18,11 @@
handle_function(OperationID, Args, Context, Opts) ->
scoper:scope(
keyring_management,
fun() ->
try
kds_thrift_handler_utils:filter_fun_exceptions(
fun() ->
handle_function_(OperationID, Args, Context, Opts)
catch
throw:Exception ->
throw(Exception);
error:{woody_error, _} = WoodyError:Stacktrace ->
erlang:raise(error, WoodyError, Stacktrace);
Class:_Exception:Stacktrace ->
erlang:raise(Class, '***', Stacktrace)
end
end
)
).
handle_function_('StartInit', [Threshold], _Context, _Opts) ->
@ -312,4 +305,4 @@ decode_signed_share(#'cds_SignedMasterKeyShare'{
-spec raise(_) -> no_return().
raise(Exception) ->
woody_error:raise(business, Exception).
kds_thrift_handler_utils:raise(Exception).

View File

@ -16,18 +16,11 @@
handle_function(OperationID, Args, Context, Opts) ->
scoper:scope(
keyring_storage,
fun() ->
try
kds_thrift_handler_utils:filter_fun_exceptions(
fun() ->
handle_function_(OperationID, Args, Context, Opts)
catch
throw:Exception ->
throw(Exception);
error:{woody_error, _} = WoodyError:Stacktrace ->
erlang:raise(error, WoodyError, Stacktrace);
Class:_Exception:Stacktrace ->
erlang:raise(Class, '***', Stacktrace)
end
end
)
).
handle_function_('GetKeyring', [], _Context, _Opts) ->
@ -74,4 +67,4 @@ encode_keys(Keys, KeysMeta) ->
-spec raise(_) -> no_return().
raise(Exception) ->
woody_error:raise(business, Exception).
kds_thrift_handler_utils:raise(Exception).

View File

@ -0,0 +1,57 @@
-module(kds_thrift_handler_utils).
-export([filter_fun_exceptions/1]).
-export([raise/1]).
%%
%% API
%%
-spec filter_fun_exceptions(fun()) -> fun().
filter_fun_exceptions(Fun) ->
fun() ->
try
Fun()
catch
throw:Exception ->
throw(Exception);
error:{woody_error, _} = WoodyError:Stacktrace ->
erlang:raise(error, WoodyError, Stacktrace);
Class:Exception:Stacktrace ->
erlang:raise(Class, filter_error_reason(Exception), Stacktrace)
end
end.
-spec raise(_) -> no_return().
raise(Exception) ->
woody_error:raise(business, Exception).
%%
%% Internals
%%
% 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) ->
maps:map(
fun(_Key, Value) ->
filter_error_reason(Value)
end,
Reason
);
filter_error_reason(Reason) when
is_atom(Reason) orelse
is_number(Reason) orelse
is_reference(Reason) orelse
is_pid(Reason)
->
Reason;
% Other
filter_error_reason(_Reason) ->
'***'.