mapError<E2> method

Result<T, E2> mapError<E2>(
  1. E2 mapFunction(
    1. E error
    )
)

Maps from a monad from one error type to another. This is useful for transforming from error mappings between APIs etc.

final Result<int, Exception> result1 = obj.doSomething();
final Result<int, String> result2 = result1.mapError((exception) => exception.message);

Implementation

Result<T, E2> mapError<E2>(E2 Function(E error) mapFunction) {
  if (isSuccess) {
    return Result.ok(_value);
  }

  final newError = mapFunction(_error);
  return Result.error(newError);
}