Current section

Files

Jump to
libero src libero_ffi.erl
Raw

src/libero_ffi.erl

%% Libero RPC panic-catching FFI.
%%
%% try_call(F) runs the zero-arg function F and returns {ok, Result}
%% on success, or {error, ReasonBinary} if the function panics or
%% throws. The reason is stringified so the caller can log it
%% alongside a trace_id without pattern-matching on arbitrary
%% Erlang term shapes.
-module(libero_ffi).
-export([try_call/1, encode/1, decode/1]).
encode(Term) ->
erlang:term_to_binary(Term).
decode(Bin) ->
erlang:binary_to_term(Bin, [safe]).
try_call(F) ->
try F() of
Result -> {ok, Result}
catch
Class:Reason:Stacktrace ->
Message = io_lib:format(
"~p: ~p~nstacktrace: ~p",
[Class, Reason, Stacktrace]
),
{error, erlang:iolist_to_binary(Message)}
end.