tryAndWrapFuture<RT extends Object, CT extends Exception, ET extends Exception> static method

Future<Result<RT, ET>> tryAndWrapFuture<RT extends Object, CT extends Exception, ET extends Exception>(
  1. Future<RT> tryAction(),
  2. ET customExceptionFactory(
    1. CT e
    )
)

RT...return type
CT...catch exception type
ET...custom exception type

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

Implementation

static Future<Result<RT, ET>> tryAndWrapFuture<RT extends Object, CT extends Exception, ET extends Exception>(Future<RT> Function() tryAction, ET Function(CT e) customExceptionFactory) async {

    final classLocation = Result;
    const functionLocation = 'tryAndWrapFuture';
    Map<String, dynamic> monitor = {};
    Map<String, dynamic> debug = {};
    List<Log> histories = [];

    try {

        final value = await tryAction();
        return Success(value, classLocation, functionLocation, monitor, debug, histories);

    } on CT catch (e) {

        final exception = customExceptionFactory(e);
        return Failure(exception, classLocation, functionLocation, monitor, debug, histories);

    }

}