var apis 		= require( './apis' );
var rongrequest = require( './rongrequest' );
var util 		= require( './util' );
var rcResult 	= require( '../const' ).result;

exports.sync = function( userId, groupIdNamePairs, format, callback ) {
	var params = { userId : userId };
	var validGroupId = true;
	for( var k in groupIdNamePairs ) {
		if( !util.validateId( k ) ) {
			console.log( k + ' is invalid' );
			validGroupId = false;
			break;
		}
		params[ 'group[' + k + ']' ] = groupIdNamePairs[k];
	}

	if( validGroupId ) {
		rongrequest.request( apis['group']['sync'], params, format, function( err, resultText ) {
			return callback( err, resultText );
		} );		
	}
	else {
		return callback( { code : rcResult.INVALID_GROUPID, message : rcResult.INVALID_GROUPID_MSG }, null );
	}
}

exports.quit = function( userIDs, groupId, format, callback ) {
	if( !util.validateId( groupId ) ) {
		return callback( 'Invalid group id', null );
	}

	var params = { groupId : groupId };
	if( typeof userIDs == 'object' && userIDs.length ) {
		rongrequest.requestWithSameFields( apis['group']['quit'], params, [ { field : 'userId', values : userIDs } ], format, callback );
	} 
	else if( typeof userIDs == 'string' || typeof userIDs == 'number' ) {
		params.userId = userIDs;
		rongrequest.request( apis['group']['quit'], params, format, callback );
	}
	else {
		throw new Error( "Invalid userId! The userId should be either a string, a number, or an non-empty array!" );
	}
}

exports.dismiss = function( userId, groupId, format, callback ) {
	if( !util.validateId( groupId ) ) {
		return callback( { code : rcResult.INVALID_GROUPID, message : rcResult.INVALID_GROUPID_MSG }, null );
	}

	var params = { userId : userId };
	rongrequest.request( apis['group']['dismiss'], {
		userId  : userId,
		groupId : groupId		
	}, format, function( err, resultText ) {
		return callback( err, resultText );
	} );
}

function createGroup( userIDs, groupId, groupName, format, callback ) {
	if( !util.validateId( groupId ) ) {
		return callback( { code : rcResult.INVALID_GROUPID, message : rcResult.INVALID_GROUPID_MSG }, null );
	}

	var params = { groupId : groupId, groupName : groupName };
	if( typeof userIDs == 'object' && userIDs.length ) {
		rongrequest.requestWithSameFields( apis['group']['create'], params, [ { field : 'userId', values : userIDs } ], format, callback );
	} 
	else if( typeof userIDs == 'string' || typeof userIDs == 'number' ) {
		params.userId = userIDs;
		rongrequest.request( apis['group']['create'], params, format, callback );
	}
	else {
		throw new Error( "Invalid userId! The userId should be either a string, a number, or an non-empty array!" );
	}
}


exports.create = createGroup;
exports.join   = createGroup;
@@ -1332,39 +1332,39 @@
 %7D%0A%09else %7B%0A%09%09
-throw new Error
+return callback
 ( %22Invalid u
@@ -2538,23 +2538,23 @@
 %7B%0A%09%09
-throw new Error
+return callback
 ( %22I
@@ -2697,20 +2697,21 @@
 oin   = createGroup;
+%0A

Modify the way to handle the invalid userId in group API, return the error message with callback, rather than throw an error, which should be more friendly.