guarded<T> static method

Future<Result<T, AppError>> guarded<T>(
  1. Future<T> action()
)

Handle exception and convert to Result Example

class SimpleApi {

This will catch any exception thrown by the action and convert it to an `AppError

Implementation

//   Future<Result<User, AppError>> getUser() {
//     return ResultFutureExtension.guarded(() async {
//       await Future.delayed(const Duration(seconds: 1));
//       return User(name: 'John Doe', email: 'john.doe@example.com');
//     });
//   }
// }
/// ```
/// This will catch any exception thrown by the action and convert it to an `AppError
static Future<Result<T, AppError>> guarded<T>(
  Future<T> Function() action,
) async {
  try {
    final result = await action();
    return Ok(result);
  } catch (e) {
    return Err(
      ExceptionHandler.handle(e is Exception ? e : Exception(e.toString())),
    );
  }
}