diff --git a/Parse/Internal/Commands/PFRESTCommand.m b/Parse/Internal/Commands/PFRESTCommand.m index 4bebec54a..b9460fa58 100644 --- a/Parse/Internal/Commands/PFRESTCommand.m +++ b/Parse/Internal/Commands/PFRESTCommand.m @@ -10,6 +10,7 @@ #import "PFRESTCommand.h" #import "PFRESTCommand_Private.h" +#import "PFAssert.h" #import "PFCoreManager.h" #import "PFFieldOperation.h" #import "PFHTTPRequest.h" @@ -159,10 +160,8 @@ - (void)maybeChangeServerOperation { } } - if ([self.httpMethod isEqualToString:PFHTTPRequestMethodDELETE] && !objectId) { - [NSException raise:NSInternalInconsistencyException - format:@"Attempt to delete non-existent object."]; - } + PFConsistencyAssert(![self.httpMethod isEqualToString:PFHTTPRequestMethodDELETE] || objectId, + @"Attempt to delete non-existent object."); } } diff --git a/Parse/Internal/Commands/PFRESTQueryCommand.m b/Parse/Internal/Commands/PFRESTQueryCommand.m index f452fb660..bf90f49f1 100644 --- a/Parse/Internal/Commands/PFRESTQueryCommand.m +++ b/Parse/Internal/Commands/PFRESTQueryCommand.m @@ -9,6 +9,7 @@ #import "PFRESTQueryCommand.h" +#import "PFAssert.h" #import "PFEncoder.h" #import "PFHTTPRequest.h" #import "PFQueryPrivate.h" @@ -133,30 +134,11 @@ + (NSDictionary *)findCommandParametersWithOrder:(NSString *)order NSMutableArray *newArray = [NSMutableArray array]; for (PFQuery *subquery in array) { // TODO: (nlutsenko) Move this validation into PFQuery/PFQueryState. - if (subquery.state.limit >= 0) { - [NSException raise:NSInvalidArgumentException - format:@"OR queries do not support sub queries with limits"]; - } - - if (subquery.state.skip > 0) { - [NSException raise:NSInvalidArgumentException - format:@"OR queries do not support sub queries with skip"]; - } - - if ([subquery.state.sortKeys count]) { - [NSException raise:NSInvalidArgumentException - format:@"OR queries do not support sub queries with order"]; - } - - if ([subquery.state.includedKeys count] > 0) { - [NSException raise:NSInvalidArgumentException - format:@"OR queries do not support sub-queries with includes"]; - } - - if (subquery.state.selectedKeys) { - [NSException raise:NSInvalidArgumentException - format:@"OR queries do not support sub-queries with selectKeys"]; - } + PFParameterAssert(subquery.state.limit < 0, @"OR queries do not support sub queries with limits"); + PFParameterAssert(subquery.state.skip == 0, @"OR queries do not support sub queries with skip"); + PFParameterAssert(subquery.state.sortKeys.count == 0, @"OR queries do not support sub queries with order"); + PFParameterAssert(subquery.state.includedKeys.count == 0, @"OR queries do not support sub-queries with includes"); + PFParameterAssert(subquery.state.selectedKeys == nil, @"OR queries do not support sub-queries with selectKeys"); NSDictionary *queryDict = [self findCommandParametersWithOrder:subquery.state.sortOrderString conditions:subquery.state.conditions diff --git a/Parse/Internal/FieldOperation/PFFieldOperation.m b/Parse/Internal/FieldOperation/PFFieldOperation.m index 6bfcffbf1..fe828f7ec 100644 --- a/Parse/Internal/FieldOperation/PFFieldOperation.m +++ b/Parse/Internal/FieldOperation/PFFieldOperation.m @@ -152,11 +152,9 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous { NSNumber *newAmount = [PFInternalUtils addNumber:self.amount withNumber:((PFIncrementOperation *)previous).amount]; return [PFIncrementOperation incrementWithAmount:newAmount]; - } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"Operation is invalid after previous operation." - userInfo:nil]; } + [NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."]; + return nil; } - (id)applyToValue:(id)oldValue forKey:(NSString *)key { @@ -210,19 +208,16 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous { NSArray *newArray = [oldArray arrayByAddingObjectsFromArray:self.objects]; return [PFSetOperation setWithValue:newArray]; } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"You can't add an item to a non-array." - userInfo:nil]; + [NSException raise:NSInternalInconsistencyException format:@"You can't add an item to a non-array."]; + return nil; } } else if ([previous isKindOfClass:[PFAddOperation class]]) { NSMutableArray *newObjects = [((PFAddOperation *)previous).objects mutableCopy]; [newObjects addObjectsFromArray:self.objects]; return [[self class] addWithObjects:newObjects]; - } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"Operation is invalid after previous operation." - userInfo:nil]; } + [NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."]; + return nil; } - (id)applyToValue:(id)oldValue forKey:(NSString *)key { @@ -230,11 +225,9 @@ - (id)applyToValue:(id)oldValue forKey:(NSString *)key { return [self.objects mutableCopy]; } else if ([oldValue isKindOfClass:[NSArray class]]) { return [((NSArray *)oldValue)arrayByAddingObjectsFromArray:self.objects]; - } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"Operation is invalid after previous operation." - userInfo:nil]; } + [NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."]; + return nil; } @end @@ -274,18 +267,15 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous { NSArray *oldArray = (((PFSetOperation *)previous).value); return [PFSetOperation setWithValue:[self applyToValue:oldArray forKey:nil]]; } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"You can't add an item to a non-array." - userInfo:nil]; + [NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."]; + return nil; } } else if ([previous isKindOfClass:[PFAddUniqueOperation class]]) { NSArray *previousObjects = ((PFAddUniqueOperation *)previous).objects; return [[self class] addUniqueWithObjects:[self applyToValue:previousObjects forKey:nil]]; - } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"Operation is invalid after previous operation." - userInfo:nil]; } + [NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."]; + return nil; } - (id)applyToValue:(id)oldValue forKey:(NSString *)key { @@ -311,11 +301,9 @@ - (id)applyToValue:(id)oldValue forKey:(NSString *)key { } } return newValue; - } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"Operation is invalid after previous operation." - userInfo:nil]; } + [NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."]; + return nil; } @end @@ -348,26 +336,23 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous { if (!previous) { return self; } else if ([previous isKindOfClass:[PFDeleteOperation class]]) { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"You can't remove items from a deleted array." - userInfo:nil]; + [NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."]; + return nil; } else if ([previous isKindOfClass:[PFSetOperation class]]) { if ([((PFSetOperation *)previous).value isKindOfClass:[NSArray class]]) { NSArray *oldArray = ((PFSetOperation *)previous).value; return [PFSetOperation setWithValue:[self applyToValue:oldArray forKey:nil]]; } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"You can't add an item to a non-array." - userInfo:nil]; + [NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."]; + return nil; } } else if ([previous isKindOfClass:[PFRemoveOperation class]]) { NSArray *newObjects = [((PFRemoveOperation *)previous).objects arrayByAddingObjectsFromArray:self.objects]; return [PFRemoveOperation removeWithObjects:newObjects]; - } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"Operation is invalid after previous operation." - userInfo:nil]; } + + [NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."]; + return nil; } - (id)applyToValue:(id)oldValue forKey:(NSString *)key { @@ -393,11 +378,9 @@ - (id)applyToValue:(id)oldValue forKey:(NSString *)key { } } return newValue; - } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"Operation is invalid after previous operation." - userInfo:nil]; } + [NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."]; + return nil; } @end @@ -426,10 +409,8 @@ + (instancetype)addRelationToObjects:(NSArray *)targets { } for (PFObject *target in targets) { - if (![[target parseClassName] isEqualToString:op.targetClass]) { - [NSException raise:NSInvalidArgumentException - format:@"All objects in a relation must be of the same class."]; - } + PFParameterAssert([target.parseClassName isEqualToString:op.targetClass], + @"All objects in a relation must be of the same class."); [op.relationsToAdd addObject:target]; } @@ -443,10 +424,8 @@ + (instancetype)removeRelationToObjects:(NSArray *)targets { } for (PFObject *target in targets) { - if (![[target parseClassName] isEqualToString:operation.targetClass]) { - [NSException raise:NSInvalidArgumentException - format:@"All objects in a relation must be of the same class."]; - } + PFParameterAssert([target.parseClassName isEqualToString:operation.targetClass], + @"All objects in a relation must be of the same class."); [operation.relationsToRemove addObject:target]; } @@ -496,57 +475,48 @@ - (id)encodeWithObjectEncoder:(PFEncoder *)objectEncoder { return removeDict; } - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"A PFRelationOperation was created without any data." - userInfo:nil]; + [NSException raise:NSInternalInconsistencyException format:@"A PFRelationOperation was created without any data."]; + return nil; } - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous { if (!previous) { return self; - } else if ([previous isKindOfClass:[PFDeleteOperation class]]) { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"You can't modify a relation after deleting it." - userInfo:nil]; - } else if ([previous isKindOfClass:[PFRelationOperation class]]) { - PFRelationOperation *previousOperation = (PFRelationOperation *)previous; - if (previousOperation.targetClass && - ![previousOperation.targetClass isEqualToString:self.targetClass]) { - [NSException raise:NSInvalidArgumentException - format:@"Related object object must be of class %@, but %@ was passed in", - previousOperation.targetClass, - self.targetClass]; - } else { - //TODO: (nlutsenko) This logic seems to be messed up. We should return a new operation here, also merging logic seems funky. - NSSet *newRelationsToAdd = [self.relationsToAdd copy]; - NSSet *newRelationsToRemove = [self.relationsToRemove copy]; - [self.relationsToAdd removeAllObjects]; - [self.relationsToRemove removeAllObjects]; - - for (NSString *objectId in previousOperation.relationsToAdd) { - [self.relationsToRemove removeObject:objectId]; - [self.relationsToAdd addObject:objectId]; - } - for (NSString *objectId in previousOperation.relationsToRemove) { - [self.relationsToRemove removeObject:objectId]; - [self.relationsToRemove addObject:objectId]; - } + } - for (NSString *objectId in newRelationsToAdd) { - [self.relationsToRemove removeObject:objectId]; - [self.relationsToAdd addObject:objectId]; - } - for (NSString *objectId in newRelationsToRemove) { - [self.relationsToRemove removeObject:objectId]; - [self.relationsToRemove addObject:objectId]; - } - } - return self; - } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"Operation is invalid after previous operation." - userInfo:nil]; + PFConsistencyAssert(![previous isKindOfClass:[PFDeleteOperation class]], @"You can't modify a relation after deleting it"); + PFConsistencyAssert([previous isKindOfClass:[PFRelationOperation class]], @"Operation is invalid after previous operation"); + + PFRelationOperation *previousOperation = (PFRelationOperation *)previous; + + PFParameterAssert(!previousOperation.targetClass || [previousOperation.targetClass isEqualToString:self.targetClass], + @"Related object object must be of class %@, but %@ was passed in", + previousOperation.targetClass, self.targetClass); + + //TODO: (nlutsenko) This logic seems to be messed up. We should return a new operation here, also merging logic seems funky. + NSSet *newRelationsToAdd = [self.relationsToAdd copy]; + NSSet *newRelationsToRemove = [self.relationsToRemove copy]; + [self.relationsToAdd removeAllObjects]; + [self.relationsToRemove removeAllObjects]; + + for (NSString *objectId in previousOperation.relationsToAdd) { + [self.relationsToRemove removeObject:objectId]; + [self.relationsToAdd addObject:objectId]; } + for (NSString *objectId in previousOperation.relationsToRemove) { + [self.relationsToRemove removeObject:objectId]; + [self.relationsToRemove addObject:objectId]; + } + + for (NSString *objectId in newRelationsToAdd) { + [self.relationsToRemove removeObject:objectId]; + [self.relationsToAdd addObject:objectId]; + } + for (NSString *objectId in newRelationsToRemove) { + [self.relationsToRemove removeObject:objectId]; + [self.relationsToRemove addObject:objectId]; + } + return self; } - (id)applyToValue:(id)oldValue forKey:(NSString *)key { @@ -557,20 +527,16 @@ - (id)applyToValue:(id)oldValue forKey:(NSString *)key { relation = oldValue; if (self.targetClass) { if (relation.targetClass) { - if (![relation.targetClass isEqualToString:targetClass]) { - [NSException raise:NSInvalidArgumentException - format:@"Related object object must be of class %@, but %@ was passed in", - relation.targetClass, - self.targetClass]; - } + PFParameterAssert([relation.targetClass isEqualToString:targetClass], + @"Related object object must be of class %@, but %@ was passed in", + relation.targetClass, self.targetClass); } else { relation.targetClass = self.targetClass; } } } else { - @throw [NSException exceptionWithName:NSInternalInconsistencyException - reason:@"Operation is invalid after previous operation." - userInfo:nil]; + [NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."]; + return nil; } for (PFObject *object in self.relationsToAdd) { diff --git a/Parse/Internal/LocalDataStore/OfflineQueryLogic/PFOfflineQueryLogic.m b/Parse/Internal/LocalDataStore/OfflineQueryLogic/PFOfflineQueryLogic.m index 5f6379360..63168cec4 100644 --- a/Parse/Internal/LocalDataStore/OfflineQueryLogic/PFOfflineQueryLogic.m +++ b/Parse/Internal/LocalDataStore/OfflineQueryLogic/PFOfflineQueryLogic.m @@ -133,9 +133,7 @@ - (id)valueForContainer:(id)container PFObject *object = (PFObject *)container; // The object needs to have been fetched already if we are going to sort by one of its field. - if (!object.isDataAvailable) { - [NSException raise:NSInvalidArgumentException format:@"Bad key %@", key]; - } + PFParameterAssert(object.isDataAvailable, @"Bad key %@", key); // Handle special keys for PFObject. if ([key isEqualToString:@"objectId"]) { @@ -336,9 +334,9 @@ + (BOOL)matchesValue:(id)value if (options == nil) { options = @""; } - if ([options rangeOfString:@"^[imxs]*$" options:NSRegularExpressionSearch].location == NSNotFound) { - [NSException raise:NSInvalidArgumentException format:@"Invalid regex options: %@", options]; - } + + PFParameterAssert([options rangeOfString:@"^[imxs]*$" options:NSRegularExpressionSearch].location != NSNotFound, + @"Invalid regex options %@", options); NSRegularExpressionOptions flags = 0; if ([options rangeOfString:@"i"].location != NSNotFound) { @@ -402,19 +400,13 @@ + (BOOL)matchesValue:(id)value PFGeoPoint *northEast = box[1]; PFGeoPoint *target = (PFGeoPoint *)value; - if (northEast.longitude < southWest.longitude) { - [NSException raise:NSInvalidArgumentException - format:@"whereWithinGeoBox queries cannot cross the International Date Line."]; - } - if (northEast.latitude < southWest.latitude) { - [NSException raise:NSInvalidArgumentException - format:@"The southwest corner of a geo box must be south of the northeast corner."]; - } - if (northEast.longitude - southWest.longitude > 180) { - [NSException raise:NSInvalidArgumentException - format:@"Geo box queries larger than 180 degrees in longitude are not supported." - @"Please check point order."]; - } + PFParameterAssert(northEast.longitude >= southWest.longitude, + @"whereWithinGeoBox queries cannot cross the International Date Line."); + PFParameterAssert(northEast.latitude >= southWest.latitude, + @"The southwest corner of a geo box must be south of the northeast corner."); + PFParameterAssert((northEast.longitude - southWest.longitude) <= 180, + @"Geo box queries larger than 180 degrees in longitude are not supported." + @"Please check point order."); return (target.latitude >= southWest.latitude && target.latitude <= northEast.latitude && @@ -826,10 +818,8 @@ - (void)_sortResults:(NSMutableArray *)results ofQueryState:(PFQueryState *)quer [keys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSString *key = (NSString *)obj; if ([key rangeOfString:@"^-?[A-Za-z][A-Za-z0-9_]*$" options:NSRegularExpressionSearch].location == NSNotFound) { - if (![@"_created_at" isEqualToString:key] && ![@"_updated_at" isEqualToString:key]) { - [NSException raise:NSInternalInconsistencyException - format:@"Invalid key name: %@", key]; - } + PFConsistencyAssert([@"_created_at" isEqualToString:key] || [@"_updated_at" isEqualToString:key], + @"Invalid key name: %@", key); } }]; diff --git a/Parse/Internal/LocalDataStore/OfflineStore/PFOfflineStore.m b/Parse/Internal/LocalDataStore/OfflineStore/PFOfflineStore.m index 899045c8c..d90e373b0 100644 --- a/Parse/Internal/LocalDataStore/OfflineStore/PFOfflineStore.m +++ b/Parse/Internal/LocalDataStore/OfflineStore/PFOfflineStore.m @@ -944,10 +944,8 @@ - (void)updateObjectIdForObject:(PFObject *)object oldObjectId:(NSString *)oldObjectId newObjectId:(NSString *)newObjectId { if (oldObjectId != nil) { - if ([oldObjectId isEqualToString:newObjectId]) { - return; - } - [NSException raise:NSInternalInconsistencyException format:@"objectIds cannot be changed in offline mode."]; + PFConsistencyAssert([oldObjectId isEqualToString:newObjectId], @"objectIds cannot be changed in offline mode."); + return; } NSString *className = object.parseClassName; diff --git a/Parse/Internal/Object/LocalIdStore/PFObjectLocalIdStore.m b/Parse/Internal/Object/LocalIdStore/PFObjectLocalIdStore.m index e615e425e..7c88c3e41 100644 --- a/Parse/Internal/Object/LocalIdStore/PFObjectLocalIdStore.m +++ b/Parse/Internal/Object/LocalIdStore/PFObjectLocalIdStore.m @@ -140,10 +140,7 @@ + (BOOL)isLocalId:(NSString *)localId { * Grabs one entry in the local id map off the disk. */ - (PFObjectLocalIdStoreMapEntry *)getMapEntry:(NSString *)localId { - if (![[self class] isLocalId:localId]) { - [NSException raise:NSInternalInconsistencyException - format:@"Tried to get invalid local id: \"%@\".", localId]; - } + PFConsistencyAssert([[self class] isLocalId:localId], @"Tried to get invalid local id: \"%@\".", localId); PFObjectLocalIdStoreMapEntry *entry = nil; @@ -173,10 +170,7 @@ - (PFObjectLocalIdStoreMapEntry *)getMapEntry:(NSString *)localId { * Writes one entry to the local id map on disk. */ - (void)putMapEntry:(PFObjectLocalIdStoreMapEntry *)entry forLocalId:(NSString *)localId { - if (![[self class] isLocalId:localId]) { - [NSException raise:NSInternalInconsistencyException - format:@"Tried to get invalid local id: \"%@\".", localId]; - } + PFConsistencyAssert([[self class] isLocalId:localId], @"Tried to get invalid local id: \"%@\".", localId); NSString *file = [_diskPath stringByAppendingPathComponent:localId]; [entry writeToFile:file]; @@ -186,10 +180,7 @@ - (void)putMapEntry:(PFObjectLocalIdStoreMapEntry *)entry forLocalId:(NSString * * Removes an entry from the local id map on disk. */ - (void)removeMapEntry:(NSString *)localId { - if (![[self class] isLocalId:localId]) { - [NSException raise:NSInternalInconsistencyException - format:@"Tried to get invalid local id: \"%@\".", localId]; - } + PFConsistencyAssert([[self class] isLocalId:localId], @"Tried to get invalid local id: \"%@\".", localId); NSString *file = [_diskPath stringByAppendingPathComponent:localId]; [[NSFileManager defaultManager] removeItemAtPath:file error:nil]; @@ -207,10 +198,7 @@ - (NSString *)createLocalId { uint64_t localIdNumber = (((uint64_t)arc4random()) << 32) | ((uint64_t)arc4random()); NSString *localId = [NSString stringWithFormat:@"local_%016llx", localIdNumber]; - if (![[self class] isLocalId:localId]) { - [NSException raise:NSInternalInconsistencyException - format:@"Generated an invalid local id: \"%@\".", localId]; - } + PFConsistencyAssert([[self class] isLocalId:localId], @"Generated an invalid local id: \"%@\".", localId); return localId; } diff --git a/Parse/Internal/PFJSONSerialization.m b/Parse/Internal/PFJSONSerialization.m index 5958019cb..682c1244c 100644 --- a/Parse/Internal/PFJSONSerialization.m +++ b/Parse/Internal/PFJSONSerialization.m @@ -9,6 +9,7 @@ #import "PFJSONSerialization.h" +#import "PFAssert.h" #import "PFLogging.h" @implementation PFJSONSerialization @@ -16,10 +17,8 @@ @implementation PFJSONSerialization + (NSData *)dataFromJSONObject:(id)object { NSError *error = nil; NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error]; - if (!data || error != nil) { - [NSException raise:NSInvalidArgumentException - format:@"PFObject values must be serializable to JSON"]; - } + PFParameterAssert(data && !error, @"PFObject values must be serializable to JSON"); + return data; } diff --git a/Parse/Internal/Query/Utilities/PFQueryUtilities.m b/Parse/Internal/Query/Utilities/PFQueryUtilities.m index 2189e171d..5e9b7172d 100644 --- a/Parse/Internal/Query/Utilities/PFQueryUtilities.m +++ b/Parse/Internal/Query/Utilities/PFQueryUtilities.m @@ -9,6 +9,7 @@ #import "PFQueryUtilities.h" +#import "PFAssert.h" #import "PFConstants.h" #import "PFErrorUtilities.h" @@ -195,20 +196,15 @@ + (NSPredicate *)removeBetween:(NSPredicate *)predicate { NSComparisonPredicate *between = (NSComparisonPredicate *)predicate; NSExpression *rhs = between.rightExpression; - if (rhs.expressionType != NSConstantValueExpressionType && - rhs.expressionType != NSAggregateExpressionType) { - [NSException raise:NSInternalInconsistencyException - format:@"The right-hand side of a BETWEEN operation must be a value or literal."]; - } - if (![rhs.constantValue isKindOfClass:[NSArray class]]) { - [NSException raise:NSInternalInconsistencyException - format:@"The right-hand side of a BETWEEN operation must be an array."]; - } + PFConsistencyAssert(rhs.expressionType == NSConstantValueExpressionType || + rhs.expressionType == NSAggregateExpressionType, + @"The right-hand side of a BETWEEN operation must be a value or literal."); + + PFConsistencyAssert([rhs.constantValue isKindOfClass:[NSArray class]], + @"The right-hand side of a BETWEEN operation must be an array."); + NSArray *array = rhs.constantValue; - if (array.count != 2) { - [NSException raise:NSInternalInconsistencyException - format:@"The right-hand side of a BETWEEN operation must have 2 items."]; - } + PFConsistencyAssert(array.count == 2, @"The right-hand side of a BETWEEN operation must have 2 items."); id minValue = array[0]; id maxValue = array[1]; @@ -406,11 +402,9 @@ + (void)assertNoPredicateModifiers:(NSPredicate *)predicate { [self _mapPredicate:predicate compoundBlock:nil comparisonBlock:^NSPredicate *(NSComparisonPredicate *comparison) { - if (comparison.comparisonPredicateModifier != NSDirectPredicateModifier) { - [NSException raise:NSInternalInconsistencyException - format:@"Unsupported comparison predicate modifier %zd.", - comparison.comparisonPredicateModifier]; - } + PFConsistencyAssert(comparison.comparisonPredicateModifier == NSDirectPredicateModifier, + @"Unsupported comparison predicate modifier %zd.", + comparison.comparisonPredicateModifier); return comparison; }]; } diff --git a/Parse/PFUser.m b/Parse/PFUser.m index 7d88ecaa4..3f89e52a3 100644 --- a/Parse/PFUser.m +++ b/Parse/PFUser.m @@ -94,11 +94,7 @@ + (BFTask *)_getCurrentUserSessionTokenAsync { // Check security on delete - (void)checkDeleteParams { - if (![self isAuthenticated]) { - [NSException raise:NSInternalInconsistencyException - format:@"User cannot be deleted unless they have been authenticated via logIn or signUp", nil]; - } - + PFConsistencyAssert(self.isAuthenticated, @"User cannot be deleted unless they have been authenticated via logIn or signUp"); [super checkDeleteParams]; } @@ -115,33 +111,22 @@ + (void)_assertValidInstanceClassName:(NSString *)className { // Checks the properties on the object before saving. - (void)_checkSaveParametersWithCurrentUser:(PFUser *)currentUser { @synchronized ([self lock]) { - if (!self.objectId && !self.isLazy) { - [NSException raise:NSInternalInconsistencyException - format:@"User cannot be saved unless they are already signed up. Call signUp first.", nil]; - } + PFConsistencyAssert(self.objectId || self.isLazy, + @"User cannot be saved unless they are already signed up. Call signUp first."); - if (![self _isAuthenticatedWithCurrentUser:currentUser] - && ![self.objectId isEqualToString:currentUser.objectId]) { - [NSException raise:NSInternalInconsistencyException - format:@"User cannot be saved unless they have been authenticated via logIn or signUp", nil]; - } + PFConsistencyAssert([self _isAuthenticatedWithCurrentUser:currentUser] || + [self.objectId isEqualToString:currentUser.objectId], + @"User cannot be saved unless they have been authenticated via logIn or signUp", nil); } } // Checks the properties on the object before signUp. - (void)checkSignUpParams { @synchronized ([self lock]) { - if (self.username == nil) { - [NSException raise:NSInternalInconsistencyException format:@"Cannot sign up without a username."]; - } - - if (self.password == nil) { - [NSException raise:NSInternalInconsistencyException format:@"Cannot sign up without a password."]; - } + PFConsistencyAssert(self.username, @"Cannot sign up without a username."); + PFConsistencyAssert(self.password, @"Cannot sign up without a password."); - if (![self isDirty:NO] || self.objectId) { - [NSException raise:NSInternalInconsistencyException format:@"Cannot sign up an existing user."]; - } + PFConsistencyAssert([self isDirty:NO] && !self.objectId, @"Cannot sign up an existing user."); } }