tryAndConvertFuture<VT extends Object, CET extends Exception, RET extends Exception> static method

FutureDanger<VT, RET> tryAndConvertFuture<VT extends Object, CET extends Exception, RET extends Exception>(
  1. Future<VT> tryAction(),
  2. RET returnExceptionFactory(
    1. CET e
    ),
  3. Log log
)

VT...value type
CET...catch exception type
RET...return exception type

exception or failure を throw する可能性のある asynchronous function を実行し, もし exception が throw された 場合 その exception を catch し, 新しい exception に convert する sugar constructor.

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

Implementation

static FutureDanger<VT, RET> tryAndConvertFuture<
    VT extends Object
    ,CET extends Exception
    ,RET extends Exception
>(
    Future<VT> Function() tryAction,
    RET Function(CET e) returnExceptionFactory,
    Log log,
) async {

    try {

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

    } on CET catch (e) {

        final exception = returnExceptionFactory(e);
        // convert 前の exception を log に記録する.
        return Failure(exception, log.monitor({'exception': e}));

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

        // log 情報をしっかり受け取る.
        log.add(e);
        final exception = returnExceptionFactory(e.wrapped);
        return Failure(exception, log);

    }

}