executeReaction method

void executeReaction(
  1. BuildContext context,
  2. String errorCode,
  3. [String? route]
)

Call a reaction depending on the errorCode with the provided context If the error code is not associated with a reaction, the function does nothing

Implementation

void executeReaction(BuildContext context, String errorCode,
    [String? route]) {
  final ApiBase apiBase = ApiBase.read(context);
  switch ((errorCode, route)) {
    // Back-end errors
    // Login category (10)
    // Invalid refresh token
    case (invalidOrExpiredToken, _):
    case (userRefreshInvalidTokenUserUUID, _):
    case (userRefreshInvalidTokenUID, _):
    case (userRefreshUserNotFound, _):
      Future.wait(
              [_storage.read(key: "email"), _storage.read(key: "password")])
          .then((value) {
        if (value[0] != null && value[1] != null) {
          apiBase
              .login(value[0]!, value[1]!)
              .then((result) {
                if (result.isLeft) {
                  _invalidLoginRedirect(context, _storage);
                }
                return result;
              })
              .mapRight((_) => apiBase.getMyProfile().mapRight((user) {
                    if (context.mounted) {
                      context.read<AppState>().userId = user.id;
                    }
                  }))
              .onError((error, _) {
                _invalidLoginRedirect(context, _storage);
                return Left(ApiError(
                    httpErrorCode: 400,
                    apiStatusCode: "AMBXXXX",
                    message: error.toString())); // Ensure a return value
              });
          return;
        }
        Navigator.push(
            context, MaterialPageRoute(builder: (_) => const LoginFrame()));
      });
    default:
      return;
  }
}