exceptionFor method

UserException? exceptionFor(
  1. Object actionTypeOrList
)

Returns the UserException of the actionTypeOrList that failed.

actionTypeOrList can be a Type, or an Iterable of types. Any other type of object will return null and throw a StoreException after the async gap.

Note: This method uses the EXACT type in actionTypeOrList. Subtypes are not considered.

Implementation

UserException? exceptionFor(Object actionTypeOrList) {
  //
  // 1) If a type was passed:
  if (actionTypeOrList is Type) {
    _actionsWeCanCheckFailed.add(actionTypeOrList);
    var action = _failedActions[actionTypeOrList];
    var error = action?.status.wrappedError;
    return (error is UserException) ? error : null;
  }
  //
  // 2) If a list was passed:
  else if (actionTypeOrList is Iterable) {
    for (var actionType in actionTypeOrList) {
      _actionsWeCanCheckFailed.add(actionType);
      if (actionType is Type) {
        var error = _failedActions.entries
            .firstWhereOrNull((entry) => entry.key == actionType)
            ?.value
            .status
            .wrappedError;
        return (error is UserException) ? error : null;
      } else {
        Future.microtask(() {
          throw StoreException("You can't do exceptionFor([${actionTypeOrList.runtimeType}]), "
              "but only an action Type, or a List of types.");
        });
      }
    }
    return null;
  }
  // 3) If something different was passed, it's an error. We show the error after the
  // async gap, so we don't interrupt the code. But we return null.
  else {
    Future.microtask(() {
      throw StoreException("You can't do exceptionFor(${actionTypeOrList.runtimeType}), "
          "but only an action Type, or a List of types.");
    });

    return null;
  }
}