tryAndCatchFuture<RT extends Object, ET extends Exception> static method

FutureDanger<RT, ET> tryAndCatchFuture<RT extends Object, ET extends Exception>(
  1. Future<RT> tryAction(),
  2. Log log
)

RT...return type
ET...exception type

tryAction を実行し, もし exception or Failure が throw された 場合 それ を catch し 返す syntax sugar.

catch した Failure の log を渡すために log は必須.

Implementation

static FutureDanger<RT, ET> tryAndCatchFuture<
    RT extends Object
    ,ET extends Exception
>(
    Future<RT> Function() tryAction,
    Log log,
) async {

    try {

        final value = await tryAction();
        return Success(value, log);

    } on ET catch (e) {

        return Failure(e, log);

    } on Failure<Object, ET> catch (e) {

        log.add(e);
        return Failure(e.wrapped, log);

    }

}