Skip to content

Commit f15e1d1

Browse files
committed
handle MissingRequiredFieldError exception (code 135)
1 parent c418353 commit f15e1d1

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

Parse/PFConstants.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ typedef NS_ENUM(NSInteger, PFErrorCode) {
219219
The email address was invalid.
220220
*/
221221
kPFErrorInvalidEmailAddress = 125,
222+
/**
223+
Required field is missing.
224+
*/
225+
kPFErrorMissingRequiredField = 135,
222226
/**
223227
A unique field was given a value that is already taken.
224228
*/

Parse/PFInstallation.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ - (void)setChannels:(NSArray<NSString *> *)channels {
234234

235235
- (BFTask *)saveAsync:(BFTask *)toAwait {
236236
return [[super saveAsync:toAwait] continueWithBlock:^id(BFTask *task) {
237-
if (task.error.code == kPFErrorObjectNotFound) {
237+
if (task.error.code == kPFErrorObjectNotFound
238+
|| (task.error.code == kPFErrorMissingRequiredField && self.objectId == nil)) {
238239
@synchronized (self.lock) {
239240
// Retry the fetch as a save operation because this Installation was deleted on the server.
240241
// We always want [currentInstallation save] to succeed.

Tests/Unit/InstallationUnitTests.m

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,26 @@ - (void)testInstallationObjectIdCannotBeChanged {
2929
PFAssertThrowsInvalidArgumentException(installation[@"objectId"] = @"abc");
3030
}
3131

32-
- (void)testReSaveInstallation {
32+
- (void)testMissingRequiredFieldWhenSave {
33+
// mocking installation was deleted on the server
34+
id commandRunner = PFStrictProtocolMock(@protocol(PFCommandRunning));
35+
[Parse _currentManager].commandRunner = commandRunner;
36+
BFTask *mockedTask = [BFTask taskWithError:[NSError errorWithDomain:@"" code:kPFErrorMissingRequiredField userInfo:nil]];
37+
__block int callCount = 0;
38+
OCMStub([commandRunner runCommandAsync:[OCMArg any] withOptions:PFCommandRunningOptionRetryIfFailed])
39+
.andReturn(mockedTask)
40+
.andDo(^(NSInvocation *invocation) {
41+
callCount++;
42+
});
43+
44+
PFInstallation *installation = [PFInstallation currentInstallation];
45+
installation.deviceToken = @"11433856eed2f1285fb3aa11136718c1198ed5647875096952c66bf8cb976306";
46+
[installation save];
47+
OCMVerifyAll(commandRunner);
48+
XCTAssertEqual(2, callCount);
49+
}
50+
51+
- (void)testObjectNotFoundWhenSave {
3352

3453
// enable LDS
3554
[[Parse _currentManager]loadOfflineStoreWithOptions:0];
@@ -46,11 +65,17 @@ - (void)testReSaveInstallation {
4665

4766
BFTask *mockedTask = [BFTask taskWithError:[NSError errorWithDomain:@"" code:kPFErrorObjectNotFound userInfo:nil]];
4867

49-
OCMStub([commandRunner runCommandAsync:[OCMArg any] withOptions:PFCommandRunningOptionRetryIfFailed]).andReturn(mockedTask);
68+
__block int callCount = 0;
69+
OCMStub([commandRunner runCommandAsync:[OCMArg any] withOptions:PFCommandRunningOptionRetryIfFailed])
70+
.andReturn(mockedTask)
71+
.andDo(^(NSInvocation *invocation) {
72+
callCount++;
73+
});
5074

5175
installation.deviceToken = @"11433856eed2f1285fb3aa11136718c1198ed5647875096952c66bf8cb976306";
5276
[installation save];
5377
OCMVerifyAll(commandRunner);
78+
XCTAssertEqual(2, callCount);
5479
}
5580

5681
- (void)testInstallationImmutableFieldsCannotBeChanged {

0 commit comments

Comments
 (0)