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

Danger<VT, RET> tryAndConvert<VT extends Object, CET extends Exception, RET extends Exception>(
  1. 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 する可能性のある function を実行し, もし exception が throw された 場合 その exception を catch し, 新しい exception に convert する sugar constructor.

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

Implementation

static Danger<VT, RET> tryAndConvert<
    VT extends Object
    ,CET extends Exception
    ,RET extends Exception
>(
    VT Function() tryAction,
    RET Function(CET e) returnExceptionFactory,
    Log log,
) {

    try {

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

    // exception と failure の両方に対応する.
    // とりあえず.
    } 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);

    }

}