tryRunAsync<T> static method

Future<KResult<T?>> tryRunAsync<T>(
  1. Future<T?> operation(), {
  2. bool logError = true,
})

Runs operation and wraps the result in a KResult.success or KResult.error if it throws.

Implementation

static Future<KResult<T?>> tryRunAsync<T>(Future<T?> Function() operation, {bool logError = true}) async {
  try {
    return KResult.success(await operation());
  } catch (e, st) {
    if (logError) {
      log("Result tryRunAsync error: ${e.toString()}", error: e, stackTrace: st);
    }
    return KResult.error(e.toString(), st, logError);
  }
}