Skip to content

Commit 6d28cba

Browse files
Cleaned up usages of exceptions and asserts.
We no longer do `@throw [NSException exceptionWith...]` anywhere in the code base, replaced usages of `if (conditon) [NSException raise:...` with PFParamter/ConsistencyAssert where appropriate. This is related to #6.
1 parent bf0fc17 commit 6d28cba

File tree

9 files changed

+120
-218
lines changed

9 files changed

+120
-218
lines changed

Parse/Internal/Commands/PFRESTCommand.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "PFRESTCommand.h"
1111
#import "PFRESTCommand_Private.h"
1212

13+
#import "PFAssert.h"
1314
#import "PFCoreManager.h"
1415
#import "PFFieldOperation.h"
1516
#import "PFHTTPRequest.h"
@@ -159,10 +160,8 @@ - (void)maybeChangeServerOperation {
159160
}
160161
}
161162

162-
if ([self.httpMethod isEqualToString:PFHTTPRequestMethodDELETE] && !objectId) {
163-
[NSException raise:NSInternalInconsistencyException
164-
format:@"Attempt to delete non-existent object."];
165-
}
163+
PFConsistencyAssert(![self.httpMethod isEqualToString:PFHTTPRequestMethodDELETE] || objectId,
164+
@"Attempt to delete non-existent object.");
166165
}
167166
}
168167

Parse/Internal/Commands/PFRESTQueryCommand.m

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#import "PFRESTQueryCommand.h"
1111

12+
#import "PFAssert.h"
1213
#import "PFEncoder.h"
1314
#import "PFHTTPRequest.h"
1415
#import "PFQueryPrivate.h"
@@ -133,30 +134,11 @@ + (NSDictionary *)findCommandParametersWithOrder:(NSString *)order
133134
NSMutableArray *newArray = [NSMutableArray array];
134135
for (PFQuery *subquery in array) {
135136
// TODO: (nlutsenko) Move this validation into PFQuery/PFQueryState.
136-
if (subquery.state.limit >= 0) {
137-
[NSException raise:NSInvalidArgumentException
138-
format:@"OR queries do not support sub queries with limits"];
139-
}
140-
141-
if (subquery.state.skip > 0) {
142-
[NSException raise:NSInvalidArgumentException
143-
format:@"OR queries do not support sub queries with skip"];
144-
}
145-
146-
if ([subquery.state.sortKeys count]) {
147-
[NSException raise:NSInvalidArgumentException
148-
format:@"OR queries do not support sub queries with order"];
149-
}
150-
151-
if ([subquery.state.includedKeys count] > 0) {
152-
[NSException raise:NSInvalidArgumentException
153-
format:@"OR queries do not support sub-queries with includes"];
154-
}
155-
156-
if (subquery.state.selectedKeys) {
157-
[NSException raise:NSInvalidArgumentException
158-
format:@"OR queries do not support sub-queries with selectKeys"];
159-
}
137+
PFParameterAssert(subquery.state.limit < 0, @"OR queries do not support sub queries with limits");
138+
PFParameterAssert(subquery.state.skip == 0, @"OR queries do not support sub queries with skip");
139+
PFParameterAssert(subquery.state.sortKeys.count == 0, @"OR queries do not support sub queries with order");
140+
PFParameterAssert(subquery.state.includedKeys.count == 0, @"OR queries do not support sub-queries with includes");
141+
PFParameterAssert(subquery.state.selectedKeys == nil, @"OR queries do not support sub-queries with selectKeys");
160142

161143
NSDictionary *queryDict = [self findCommandParametersWithOrder:subquery.state.sortOrderString
162144
conditions:subquery.state.conditions

Parse/Internal/FieldOperation/PFFieldOperation.m

Lines changed: 68 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,9 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous {
152152
NSNumber *newAmount = [PFInternalUtils addNumber:self.amount
153153
withNumber:((PFIncrementOperation *)previous).amount];
154154
return [PFIncrementOperation incrementWithAmount:newAmount];
155-
} else {
156-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
157-
reason:@"Operation is invalid after previous operation."
158-
userInfo:nil];
159155
}
156+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
157+
return nil;
160158
}
161159

162160
- (id)applyToValue:(id)oldValue forKey:(NSString *)key {
@@ -210,31 +208,26 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous {
210208
NSArray *newArray = [oldArray arrayByAddingObjectsFromArray:self.objects];
211209
return [PFSetOperation setWithValue:newArray];
212210
} else {
213-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
214-
reason:@"You can't add an item to a non-array."
215-
userInfo:nil];
211+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
212+
return nil;
216213
}
217214
} else if ([previous isKindOfClass:[PFAddOperation class]]) {
218215
NSMutableArray *newObjects = [((PFAddOperation *)previous).objects mutableCopy];
219216
[newObjects addObjectsFromArray:self.objects];
220217
return [[self class] addWithObjects:newObjects];
221-
} else {
222-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
223-
reason:@"Operation is invalid after previous operation."
224-
userInfo:nil];
225218
}
219+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
220+
return nil;
226221
}
227222

228223
- (id)applyToValue:(id)oldValue forKey:(NSString *)key {
229224
if (!oldValue) {
230225
return [self.objects mutableCopy];
231226
} else if ([oldValue isKindOfClass:[NSArray class]]) {
232227
return [((NSArray *)oldValue)arrayByAddingObjectsFromArray:self.objects];
233-
} else {
234-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
235-
reason:@"Operation is invalid after previous operation."
236-
userInfo:nil];
237228
}
229+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
230+
return nil;
238231
}
239232

240233
@end
@@ -274,18 +267,15 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous {
274267
NSArray *oldArray = (((PFSetOperation *)previous).value);
275268
return [PFSetOperation setWithValue:[self applyToValue:oldArray forKey:nil]];
276269
} else {
277-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
278-
reason:@"You can't add an item to a non-array."
279-
userInfo:nil];
270+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
271+
return nil;
280272
}
281273
} else if ([previous isKindOfClass:[PFAddUniqueOperation class]]) {
282274
NSArray *previousObjects = ((PFAddUniqueOperation *)previous).objects;
283275
return [[self class] addUniqueWithObjects:[self applyToValue:previousObjects forKey:nil]];
284-
} else {
285-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
286-
reason:@"Operation is invalid after previous operation."
287-
userInfo:nil];
288276
}
277+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
278+
return nil;
289279
}
290280

291281
- (id)applyToValue:(id)oldValue forKey:(NSString *)key {
@@ -311,11 +301,9 @@ - (id)applyToValue:(id)oldValue forKey:(NSString *)key {
311301
}
312302
}
313303
return newValue;
314-
} else {
315-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
316-
reason:@"Operation is invalid after previous operation."
317-
userInfo:nil];
318304
}
305+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
306+
return nil;
319307
}
320308

321309
@end
@@ -348,26 +336,23 @@ - (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous {
348336
if (!previous) {
349337
return self;
350338
} else if ([previous isKindOfClass:[PFDeleteOperation class]]) {
351-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
352-
reason:@"You can't remove items from a deleted array."
353-
userInfo:nil];
339+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
340+
return nil;
354341
} else if ([previous isKindOfClass:[PFSetOperation class]]) {
355342
if ([((PFSetOperation *)previous).value isKindOfClass:[NSArray class]]) {
356343
NSArray *oldArray = ((PFSetOperation *)previous).value;
357344
return [PFSetOperation setWithValue:[self applyToValue:oldArray forKey:nil]];
358345
} else {
359-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
360-
reason:@"You can't add an item to a non-array."
361-
userInfo:nil];
346+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
347+
return nil;
362348
}
363349
} else if ([previous isKindOfClass:[PFRemoveOperation class]]) {
364350
NSArray *newObjects = [((PFRemoveOperation *)previous).objects arrayByAddingObjectsFromArray:self.objects];
365351
return [PFRemoveOperation removeWithObjects:newObjects];
366-
} else {
367-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
368-
reason:@"Operation is invalid after previous operation."
369-
userInfo:nil];
370352
}
353+
354+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
355+
return nil;
371356
}
372357

373358
- (id)applyToValue:(id)oldValue forKey:(NSString *)key {
@@ -393,11 +378,9 @@ - (id)applyToValue:(id)oldValue forKey:(NSString *)key {
393378
}
394379
}
395380
return newValue;
396-
} else {
397-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
398-
reason:@"Operation is invalid after previous operation."
399-
userInfo:nil];
400381
}
382+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
383+
return nil;
401384
}
402385

403386
@end
@@ -426,10 +409,8 @@ + (instancetype)addRelationToObjects:(NSArray *)targets {
426409
}
427410

428411
for (PFObject *target in targets) {
429-
if (![[target parseClassName] isEqualToString:op.targetClass]) {
430-
[NSException raise:NSInvalidArgumentException
431-
format:@"All objects in a relation must be of the same class."];
432-
}
412+
PFParameterAssert([target.parseClassName isEqualToString:op.targetClass],
413+
@"All objects in a relation must be of the same class.");
433414
[op.relationsToAdd addObject:target];
434415
}
435416

@@ -443,10 +424,8 @@ + (instancetype)removeRelationToObjects:(NSArray *)targets {
443424
}
444425

445426
for (PFObject *target in targets) {
446-
if (![[target parseClassName] isEqualToString:operation.targetClass]) {
447-
[NSException raise:NSInvalidArgumentException
448-
format:@"All objects in a relation must be of the same class."];
449-
}
427+
PFParameterAssert([target.parseClassName isEqualToString:operation.targetClass],
428+
@"All objects in a relation must be of the same class.");
450429
[operation.relationsToRemove addObject:target];
451430
}
452431

@@ -496,57 +475,49 @@ - (id)encodeWithObjectEncoder:(PFEncoder *)objectEncoder {
496475
return removeDict;
497476
}
498477

499-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
500-
reason:@"A PFRelationOperation was created without any data."
501-
userInfo:nil];
478+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
479+
return nil;
502480
}
503481

504482
- (PFFieldOperation *)mergeWithPrevious:(PFFieldOperation *)previous {
505483
if (!previous) {
506484
return self;
507-
} else if ([previous isKindOfClass:[PFDeleteOperation class]]) {
508-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
509-
reason:@"You can't modify a relation after deleting it."
510-
userInfo:nil];
511-
} else if ([previous isKindOfClass:[PFRelationOperation class]]) {
512-
PFRelationOperation *previousOperation = (PFRelationOperation *)previous;
513-
if (previousOperation.targetClass &&
514-
![previousOperation.targetClass isEqualToString:self.targetClass]) {
515-
[NSException raise:NSInvalidArgumentException
516-
format:@"Related object object must be of class %@, but %@ was passed in",
517-
previousOperation.targetClass,
518-
self.targetClass];
519-
} else {
520-
//TODO: (nlutsenko) This logic seems to be messed up. We should return a new operation here, also merging logic seems funky.
521-
NSSet *newRelationsToAdd = [self.relationsToAdd copy];
522-
NSSet *newRelationsToRemove = [self.relationsToRemove copy];
523-
[self.relationsToAdd removeAllObjects];
524-
[self.relationsToRemove removeAllObjects];
525-
526-
for (NSString *objectId in previousOperation.relationsToAdd) {
527-
[self.relationsToRemove removeObject:objectId];
528-
[self.relationsToAdd addObject:objectId];
529-
}
530-
for (NSString *objectId in previousOperation.relationsToRemove) {
531-
[self.relationsToRemove removeObject:objectId];
532-
[self.relationsToRemove addObject:objectId];
533-
}
485+
}
534486

535-
for (NSString *objectId in newRelationsToAdd) {
536-
[self.relationsToRemove removeObject:objectId];
537-
[self.relationsToAdd addObject:objectId];
538-
}
539-
for (NSString *objectId in newRelationsToRemove) {
540-
[self.relationsToRemove removeObject:objectId];
541-
[self.relationsToRemove addObject:objectId];
542-
}
543-
}
544-
return self;
545-
} else {
546-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
547-
reason:@"Operation is invalid after previous operation."
548-
userInfo:nil];
487+
PFConsistencyAssert(![previous isKindOfClass:[PFDeleteOperation class]], @"You can't modify a relation after deleting it");
488+
PFConsistencyAssert([previous isKindOfClass:[PFRelationOperation class]], @"Operation is invalid after previous operation");
489+
490+
PFRelationOperation *previousOperation = (PFRelationOperation *)previous;
491+
492+
PFParameterAssert(!previousOperation.targetClass || [previousOperation.targetClass isEqualToString:self.targetClass],
493+
@"Related object object must be of class %@, but %@ was passed in",
494+
previousOperation.targetClass,
495+
self.targetClass);
496+
497+
//TODO: (nlutsenko) This logic seems to be messed up. We should return a new operation here, also merging logic seems funky.
498+
NSSet *newRelationsToAdd = [self.relationsToAdd copy];
499+
NSSet *newRelationsToRemove = [self.relationsToRemove copy];
500+
[self.relationsToAdd removeAllObjects];
501+
[self.relationsToRemove removeAllObjects];
502+
503+
for (NSString *objectId in previousOperation.relationsToAdd) {
504+
[self.relationsToRemove removeObject:objectId];
505+
[self.relationsToAdd addObject:objectId];
549506
}
507+
for (NSString *objectId in previousOperation.relationsToRemove) {
508+
[self.relationsToRemove removeObject:objectId];
509+
[self.relationsToRemove addObject:objectId];
510+
}
511+
512+
for (NSString *objectId in newRelationsToAdd) {
513+
[self.relationsToRemove removeObject:objectId];
514+
[self.relationsToAdd addObject:objectId];
515+
}
516+
for (NSString *objectId in newRelationsToRemove) {
517+
[self.relationsToRemove removeObject:objectId];
518+
[self.relationsToRemove addObject:objectId];
519+
}
520+
return self;
550521
}
551522

552523
- (id)applyToValue:(id)oldValue forKey:(NSString *)key {
@@ -557,20 +528,16 @@ - (id)applyToValue:(id)oldValue forKey:(NSString *)key {
557528
relation = oldValue;
558529
if (self.targetClass) {
559530
if (relation.targetClass) {
560-
if (![relation.targetClass isEqualToString:targetClass]) {
561-
[NSException raise:NSInvalidArgumentException
562-
format:@"Related object object must be of class %@, but %@ was passed in",
563-
relation.targetClass,
564-
self.targetClass];
565-
}
531+
PFParameterAssert([relation.targetClass isEqualToString:targetClass],
532+
@"Related object object must be of class %@, but %@ was passed in",
533+
relation.targetClass, self.targetClass);
566534
} else {
567535
relation.targetClass = self.targetClass;
568536
}
569537
}
570538
} else {
571-
@throw [NSException exceptionWithName:NSInternalInconsistencyException
572-
reason:@"Operation is invalid after previous operation."
573-
userInfo:nil];
539+
[NSException raise:NSInternalInconsistencyException format:@"Operation is invalid after previous operation."];
540+
return nil;
574541
}
575542

576543
for (PFObject *object in self.relationsToAdd) {

0 commit comments

Comments
 (0)