, "" , , "" .
, :
noop() -> ok.
transaction([{Fun, Rollback} | Rest]) ->
try
{ok, Result} = Fun(),
[Result | transaction(Rest)]
catch Type:Reason ->
Rollback(),
erlang:raise(Type, Reason, erlang:get_stacktrace())
end;
transaction([Fun | Rest]) ->
% not every action require cleanup on error
transaction([{Fun, fun noop/0} | Rest]);
transaction([]) -> [].
main() ->
Actions = [
{fun write_file/0, fun cleanup_file/0},
{fun write_database/0, fun cleanup_database/0},
fun do_safe_thing/0,
{fun send_packet/0, fun cancel_send_packet/0},
],
transaction(Actions).
, , , - , .
, do_safe_ting/0 , noop/0, cleanup_database/0 cleanup_file/0.
, -, , , , {ok, Result} {error, Reason}. .