Skip to content

Commit cca3587

Browse files
committed
Refactor: Rename macros in PFPreconditon, more consistency on arguments
1 parent 8263fd7 commit cca3587

File tree

19 files changed

+85
-74
lines changed

19 files changed

+85
-74
lines changed

Parse/Parse/Internal/CloudCode/PFCloudCodeController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ - (BFTask *)callCloudCodeFunctionAsync:(NSString *)functionName
4949
@strongify(self);
5050
NSError *error;
5151
NSDictionary *encodedParameters = [[PFNoObjectEncoder objectEncoder] encodeObject:parameters error:&error];
52-
PFBailTaskIfError(encodedParameters, error);
52+
PFPreconditionReturnFailedTask(encodedParameters, error);
5353
PFRESTCloudCommand *command = [PFRESTCloudCommand commandForFunction:functionName
5454
withParameters:encodedParameters
5555
sessionToken:sessionToken
5656
error:&error];
57-
PFBailTaskIfError(command, error);
57+
PFPreconditionReturnFailedTask(command, error);
5858
return [self.dataSource.commandRunner runCommandAsync:command withOptions:PFCommandRunningOptionRetryIfFailed];
5959
}] continueWithSuccessBlock:^id(BFTask *task) {
6060
return ((PFCommandResult *)(task.result)).result[@"result"];

Parse/Parse/Internal/Commands/CommandRunner/URLRequestConstructor/PFCommandURLRequestConstructor.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ + (instancetype)constructorWithDataSource:(id<PFInstallationIdentifierStoreProvi
7373
}
7474
NSError *error = nil;
7575
requestParameters = [[PFPointerObjectEncoder objectEncoder] encodeObject:parameters error:&error];
76-
PFBailTaskIfError(requestParameters, error);
76+
PFPreconditionReturnFailedTask(requestParameters, error);
7777
}
7878

7979
return [PFHTTPURLRequestConstructor urlRequestWithURL:url

Parse/Parse/Internal/Commands/CommandRunner/URLSession/PFURLSessionCommandRunner.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ - (void)dealloc {
149149
return [self _performCommandRunningBlock:^id {
150150
NSError *error;
151151
BOOL success = [command resolveLocalIds:&error];
152-
PFBailTaskIfError(success, error);
152+
PFPreconditionReturnFailedTask(success, error);
153153
return [[self.requestConstructor getDataURLRequestAsyncForCommand:command] continueWithSuccessBlock:^id(BFTask <NSURLRequest *>*task) {
154154
return [_session performDataURLRequestAsync:task.result forCommand:command cancellationToken:cancellationToken];
155155
}];
@@ -172,7 +172,7 @@ - (void)dealloc {
172172

173173
NSError *error;
174174
BOOL success = [command resolveLocalIds:&error];
175-
PFBailTaskIfError(success, error);
175+
PFPreconditionReturnFailedTask(success, error);
176176
return [[self.requestConstructor getFileUploadURLRequestAsyncForCommand:command
177177
withContentType:contentType
178178
contentSourceFilePath:sourceFilePath] continueWithSuccessBlock:^id(BFTask<NSURLRequest *> *task) {

Parse/Parse/Internal/Commands/PFRESTQueryCommand.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ @implementation PFRESTQueryCommand
2424

2525
+ (nullable instancetype)findCommandForQueryState:(PFQueryState *)queryState withSessionToken:(NSString *)sessionToken error:(NSError **)error {
2626
NSDictionary *parameters = [self findCommandParametersForQueryState:queryState error:error];
27-
PFBailIfError(parameters, error, nil);
27+
PFPreconditionBailOnError(parameters, error, nil);
2828
return [self _findCommandForClassWithName:queryState.parseClassName
2929
parameters:parameters
3030
sessionToken:sessionToken
@@ -51,7 +51,7 @@ + (nullable instancetype)findCommandForClassWithName:(NSString *)className
5151
extraOptions:extraOptions
5252
tracingEnabled:trace
5353
error:error];
54-
PFBailIfError(parameters, error, nil);
54+
PFPreconditionBailOnError(parameters, error, nil);
5555
return [self _findCommandForClassWithName:className
5656
parameters:parameters
5757
sessionToken:sessionToken
@@ -68,7 +68,7 @@ + (nullable instancetype)_findCommandForClassWithName:(NSString *)className
6868
parameters:parameters
6969
sessionToken:sessionToken
7070
error:error];
71-
PFBailIfError(command, error, nil);
71+
PFPreconditionBailOnError(command, error, nil);
7272
return command;
7373
}
7474

Parse/Parse/Internal/Commands/PFRESTUserCommand.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ + (instancetype)_commandWithHTTPPath:(NSString *)path
3838
parameters:parameters
3939
sessionToken:sessionToken
4040
error:error];
41-
PFBailIfError(command, error, nil);
41+
PFPreconditionBailOnError(command, error, nil);
4242
if (revocableSessionEnabled) {
4343
command.additionalRequestHeaders = @{ PFRESTUserCommandRevocableSessionHeader :
4444
PFRESTUserCommandRevocableSessionHeaderEnabledValue};

Parse/Parse/Internal/FieldOperation/PFFieldOperation.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ - (NSArray *)_convertToArrayInSet:(NSSet *)set withObjectEncoder:(PFEncoder *)ob
452452
NSMutableArray *array = [NSMutableArray arrayWithCapacity:set.count];
453453
for (PFObject *object in set) {
454454
id encodedDict = [objectEncoder encodeObject:object error:error];
455-
PFBailIfError(encodedDict, error, nil);
455+
PFPreconditionBailOnError(encodedDict, error, nil);
456456
[array addObject:encodedDict];
457457
}
458458
return array;

Parse/Parse/Internal/LocalDataStore/OfflineStore/PFOfflineStore.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ - (instancetype)initWithFileManager:(PFFileManager *)fileManager options:(PFOffl
178178
PFOfflineStoreKeyOfJSON, PFOfflineStoreTableOfObjects, PFOfflineStoreKeyOfUUID];
179179
return [database executeQueryAsync:query withArgumentsInArray:@[ uuid ] block:^id(PFSQLiteDatabaseResult *_Nonnull result) {
180180
if (![result next]) {
181-
PFConsistencyErrorFailure(@"Attempted to find non-existent uuid %@. Please report this issue with stack traces and logs.", uuid);
181+
PFPreconditionFailure(@"Attempted to find non-existent uuid %@. Please report this issue with stack traces and logs.", uuid);
182182
}
183183
return [result stringForColumnIndex:0];
184184
}];
@@ -384,7 +384,7 @@ - (instancetype)initWithFileManager:(PFFileManager *)fileManager options:(PFOffl
384384
NSArray *operationSetUUIDs = nil;
385385
NSError *error;
386386
encoded = [object RESTDictionaryWithObjectEncoder:encoder operationSetUUIDs:&operationSetUUIDs error:&error];
387-
PFBailTaskIfError(encoded, error);
387+
PFPreconditionReturnFailedTask(encoded, error);
388388
return [encoder encodeFinished];
389389
}] continueWithSuccessBlock:^id(BFTask *task) {
390390
// Time to actually save the object
@@ -623,7 +623,7 @@ - (BFTask *)findAsyncForQueryState:(PFQueryState *)queryState
623623
NSArray *operationSetUUIDs = nil;
624624
NSError *error;
625625
dataDictionary = [object RESTDictionaryWithObjectEncoder:encoder operationSetUUIDs:&operationSetUUIDs error:&error];
626-
PFBailTaskIfError(dataDictionary, error);
626+
PFPreconditionReturnFailedTask(dataDictionary, error);
627627
return [encoder encodeFinished];
628628
}] continueWithSuccessBlock:^id(BFTask *task) {
629629
// Put it in database
@@ -912,7 +912,7 @@ - (BFTask *)findAsyncForQueryState:(PFQueryState *)queryState
912912
__block NSString *objectId = nil;
913913
return [[database executeQueryAsync:query withArgumentsInArray:@[ uuid ] block:^id(PFSQLiteDatabaseResult *result) {
914914
if (![result next]) {
915-
PFConsistencyErrorFailure(@"Attempted to find non-existent uuid %@. Please report this issue with stack traces and logs.", uuid);
915+
PFPreconditionFailure(@"Attempted to find non-existent uuid %@. Please report this issue with stack traces and logs.", uuid);
916916
}
917917

918918
className = [result stringForColumnIndex:0];

Parse/Parse/Internal/Object/BatchController/PFObjectBatchController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ - (BFTask *)fetchObjectsAsync:(NSArray *)objects withSessionToken:(NSString *)se
5757
@strongify(self);
5858
NSError *error;
5959
PFRESTCommand *command = [self _fetchCommandForObjects:objects withSessionToken:sessionToken error:&error];
60-
PFBailTaskIfError(command, error);
60+
PFPreconditionReturnFailedTask(command, error);
6161
return [self.dataSource.commandRunner runCommandAsync:command
6262
withOptions:PFCommandRunningOptionRetryIfFailed];
6363
}] continueWithSuccessBlock:^id(BFTask *task) {

Parse/Parse/Internal/Object/LocalIdStore/PFObjectLocalIdStore.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ + (BOOL)isLocalId:(NSString *)localId {
136136
*/
137137
- (PFObjectLocalIdStoreMapEntry *)getMapEntry:(NSString *)localId error:(NSError * __autoreleasing *) error {
138138

139-
PFConsistencyError(error, [[self class] isLocalId:localId], nil, @"Tried to get invalid local id: \"%@\".", localId);
139+
PFPreconditionBailAndSetError([[self class] isLocalId:localId], error, nil, @"Tried to get invalid local id: \"%@\".", localId);
140140

141141
PFObjectLocalIdStoreMapEntry *entry = nil;
142142

@@ -168,7 +168,7 @@ - (PFObjectLocalIdStoreMapEntry *)getMapEntry:(NSString *)localId error:(NSError
168168
* Writes one entry to the local id map on disk.
169169
*/
170170
- (BOOL)putMapEntry:(PFObjectLocalIdStoreMapEntry *)entry forLocalId:(NSString *)localId error:(NSError * __autoreleasing *)error {
171-
PFConsistencyError(error, [[self class] isLocalId:localId], NO, @"Tried to get invalid local id: \"%@\".", localId);
171+
PFPreconditionBailAndSetError([[self class] isLocalId:localId],error, NO, @"Tried to get invalid local id: \"%@\".", localId);
172172

173173
NSString *file = [_diskPath stringByAppendingPathComponent:localId];
174174
[entry writeToFile:file];
@@ -179,7 +179,7 @@ - (BOOL)putMapEntry:(PFObjectLocalIdStoreMapEntry *)entry forLocalId:(NSString *
179179
* Removes an entry from the local id map on disk.
180180
*/
181181
- (BOOL)removeMapEntry:(NSString *)localId error:(NSError * __autoreleasing *)error {
182-
PFConsistencyError(error, [[self class] isLocalId:localId], NO, @"Tried to get invalid local id: \"%@\".", localId);
182+
PFPreconditionBailAndSetError([[self class] isLocalId:localId], error, NO, @"Tried to get invalid local id: \"%@\".", localId);
183183

184184
NSString *file = [_diskPath stringByAppendingPathComponent:localId];
185185
[[NSFileManager defaultManager] removeItemAtPath:file error:nil];

Parse/Parse/Internal/PFAssert.h

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,51 @@ do { \
5858
} \
5959
} while(0)
6060

61+
/*
62+
Returns an error with the description, if a condition isn't met
63+
*/
64+
#define PFPrecondition(condition, description, ...) \
65+
if (!(condition)) { \
66+
return [PFErrorUtilities errorWithCode:-1 message:[NSString stringWithFormat:description, ##__VA_ARGS__]];\
67+
}
68+
69+
/**
70+
Returns a consistency error.
71+
*/
72+
#define PFPreconditionFailure(description, ...) \
73+
PFPrecondition(NO, description, ##__VA_ARGS__)
74+
6175
/**
6276
Sets a recoverable error for propagation
6377
*/
64-
#define PFConsistencyError(error, condition, rval, description, ...) \
78+
#define PFPreconditionBailAndSetError(condition, error, rval, description, ...) \
6579
if (!(condition) && error && *error == nil) { \
66-
*error = [PFErrorUtilities errorWithCode:-1 message:[NSString stringWithFormat:description, ##__VA_ARGS__]];\
67-
return rval;\
80+
*error = [PFErrorUtilities errorWithCode:-1 message:[NSString stringWithFormat:description, ##__VA_ARGS__]];\
81+
return rval;\
6882
}
6983

70-
#define PFBailIfError(condition, error, rval) \
84+
/*
85+
Returns the passed value if the condition isn't met and the *error is set
86+
*/
87+
#define PFPreconditionBailOnError(condition, error, rval) \
7188
if (!(condition) && *error) { \
72-
return rval;\
89+
return rval;\
7390
}
7491

75-
#define PFBailTaskIfError(condition, error) \
92+
/*
93+
Returns an failed task with the passed error if a contition isn't met and the error is set
94+
*/
95+
#define PFPreconditionReturnFailedTask(condition, error) \
7696
if (!(condition) && error) { \
77-
return [BFTask taskWithError:error];\
78-
}
79-
80-
#define PFPrecondition(condition, description, ...) \
81-
if (!(condition)) { \
82-
return [PFErrorUtilities errorWithCode:-1 message:[NSString stringWithFormat:description, ##__VA_ARGS__]];\
97+
return [BFTask taskWithError:error];\
8398
}
8499

100+
/*
101+
Returns a failed BFTask with an error description if the condition isn't met
102+
*/
85103
#define PFPreconditionWithTask(condition, description, ...) \
86104
if (!(condition)) { \
87-
return [BFTask taskWithError:[PFErrorUtilities errorWithCode:-1 message:[NSString stringWithFormat:description, ##__VA_ARGS__]]];\
105+
return [BFTask taskWithError:[PFErrorUtilities errorWithCode:-1 message:[NSString stringWithFormat:description, ##__VA_ARGS__]]];\
88106
}
89107

90108
/**
@@ -96,13 +114,6 @@ do {\
96114
format:description, ##__VA_ARGS__]; \
97115
} while(0)
98116

99-
/**
100-
Returns a consistency error.
101-
*/
102-
#define PFConsistencyErrorFailure(description, ...) \
103-
return [PFErrorUtilities errorWithCode:-1 message:[NSString stringWithFormat:description, ##__VA_ARGS__]];
104-
105-
106117

107118
/**
108119
Always raises `NSInternalInconsistencyException` with details

Parse/Parse/Internal/PFCommandCache.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ - (BFTask *)_saveCommandToCacheInBackground:(id<PFNetworkCommand>)command
295295

296296
NSError *error = nil;
297297
NSDictionary *JSON = [command dictionaryRepresentation:&error];
298-
PFBailTaskIfError(JSON, error);
298+
PFPreconditionReturnFailedTask(JSON, error);
299299
NSData *data = [NSJSONSerialization dataWithJSONObject:JSON
300300
options:0
301301
error:&error];

Parse/Parse/Internal/PFEventuallyPin.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ + (BFTask *)pinEventually:(PFObject *)object forCommand:(id<PFNetworkCommand>)co
9595
NSError *error;
9696
NSDictionary *commandDictionary = (type == PFEventuallyPinTypeCommand ? [command dictionaryRepresentation:&error] : nil);
9797
if (type == PFEventuallyPinTypeCommand) {
98-
PFBailTaskIfError(commandDictionary, error);
98+
PFPreconditionReturnFailedTask(commandDictionary, error);
9999
}
100100
return [self _pinEventually:object
101101
type:type

Parse/Parse/Internal/Push/Controller/PFPushController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ - (BFTask *)sendPushNotificationAsyncWithState:(PFPushState *)state
4545
@strongify(self);
4646
NSError *error;
4747
PFRESTCommand *command = [PFRESTPushCommand sendPushCommandWithPushState:state sessionToken:sessionToken error:&error];
48-
PFBailTaskIfError(command, error);
48+
PFPreconditionReturnFailedTask(command, error);
4949
return [self.commandRunner runCommandAsync:command withOptions:PFCommandRunningOptionRetryIfFailed];
5050
}] continueWithSuccessBlock:^id(BFTask *task) {
5151
return @(task.result != nil);

Parse/Parse/Internal/Query/Controller/PFQueryController.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ - (BFTask *)findObjectsAsyncForQueryState:(PFQueryState *)queryState
6464

6565
NSError *error;
6666
PFRESTCommand *command = [PFRESTQueryCommand findCommandForQueryState:queryState withSessionToken:sessionToken error:&error];
67-
PFBailTaskIfError(command, error);
67+
PFPreconditionReturnFailedTask(command, error);
6868
querySent = (queryState.trace ? [NSDate date] : nil);
6969
return [self runNetworkCommandAsync:command
7070
withCancellationToken:cancellationToken
@@ -117,9 +117,9 @@ - (BFTask *)countObjectsAsyncForQueryState:(PFQueryState *)queryState
117117
PFRESTQueryCommand *findCommand = [PFRESTQueryCommand findCommandForQueryState:queryState
118118
withSessionToken:sessionToken
119119
error:&error];
120-
PFBailTaskIfError(findCommand, error);
120+
PFPreconditionReturnFailedTask(findCommand, error);
121121
PFRESTCommand *countCommand = [PFRESTQueryCommand countCommandFromFindCommand:findCommand error:&error];
122-
PFBailTaskIfError(countCommand, error);
122+
PFPreconditionReturnFailedTask(countCommand, error);
123123
return [self runNetworkCommandAsync:countCommand
124124
withCancellationToken:cancellationToken
125125
forQueryState:queryState];

Parse/Parse/Internal/User/Controller/PFUserController.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ - (BFTask *)logInCurrentUserAsyncWithSessionToken:(NSString *)sessionToken {
5252
@strongify(self);
5353
NSError *error = nil;
5454
PFRESTCommand *command = [PFRESTUserCommand getCurrentUserCommandWithSessionToken:sessionToken error:&error];
55-
PFBailTaskIfError(command, error);
55+
PFPreconditionReturnFailedTask(command, error);
5656
return [self.commonDataSource.commandRunner runCommandAsync:command
5757
withOptions:PFCommandRunningOptionRetryIfFailed];
5858
}] continueWithSuccessBlock:^id(BFTask *task) {
@@ -87,7 +87,7 @@ - (BFTask *)logInCurrentUserAsyncWithUsername:(NSString *)username
8787
password:password
8888
revocableSession:revocableSession
8989
error:&error];
90-
PFBailTaskIfError(command, error);
90+
PFPreconditionReturnFailedTask(command, error);
9191
return [self.commonDataSource.commandRunner runCommandAsync:command
9292
withOptions:PFCommandRunningOptionRetryIfFailed];
9393
}] continueWithSuccessBlock:^id(BFTask *task) {
@@ -124,7 +124,7 @@ - (BFTask *)logInCurrentUserAsyncWithAuthType:(NSString *)authType
124124
authenticationData:authData
125125
revocableSession:revocableSession
126126
error:&error];
127-
PFBailTaskIfError(command, error);
127+
PFPreconditionReturnFailedTask(command, error);
128128
return [self.commonDataSource.commandRunner runCommandAsync:command
129129
withOptions:PFCommandRunningOptionRetryIfFailed];
130130
}] continueWithSuccessBlock:^id(BFTask *task) {
@@ -151,7 +151,7 @@ - (BFTask *)requestPasswordResetAsyncForEmail:(NSString *)email {
151151
@strongify(self);
152152
NSError *error = nil;
153153
PFRESTCommand *command = [PFRESTUserCommand resetPasswordCommandForUserWithEmail:email error:&error];
154-
PFBailTaskIfError(command, error);
154+
PFPreconditionReturnFailedTask(command, error);
155155
return [self.commonDataSource.commandRunner runCommandAsync:command
156156
withOptions:PFCommandRunningOptionRetryIfFailed];
157157
}] continueWithSuccessResult:nil];
@@ -167,7 +167,7 @@ - (BFTask *)logOutUserAsyncWithSessionToken:(NSString *)sessionToken {
167167
@strongify(self);
168168
NSError *error = nil;
169169
PFRESTCommand *command = [PFRESTUserCommand logOutUserCommandWithSessionToken:sessionToken error:&error];
170-
PFBailTaskIfError(command, error);
170+
PFPreconditionReturnFailedTask(command, error);
171171
return [self.commonDataSource.commandRunner runCommandAsync:command
172172
withOptions:PFCommandRunningOptionRetryIfFailed];
173173
}] continueWithSuccessResult:nil];

Parse/Parse/Internal/User/State/PFUserState.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ + (instancetype)stateWithState:(PFUserState *)state {
5151

5252
- (nullable NSDictionary *)dictionaryRepresentationWithObjectEncoder:(PFEncoder *)objectEncoder error:(NSError **)error {
5353
NSDictionary *representation = [super dictionaryRepresentationWithObjectEncoder:objectEncoder error:error];
54-
PFBailIfError(representation, error, nil);
54+
PFPreconditionBailOnError(representation, error, nil);
5555
NSMutableDictionary *dictionary = [representation mutableCopy];
5656
[dictionary removeObjectForKey:PFUserPasswordRESTKey];
5757
return dictionary;

0 commit comments

Comments
 (0)