Skip to content

Commit a801309

Browse files
Merge pull request #345 from ParsePlatform/richardross.file.error
Use errors instead of exceptions when creating a PFFile.
2 parents 247cc9a + 05e4990 commit a801309

File tree

3 files changed

+74
-19
lines changed

3 files changed

+74
-19
lines changed

Parse/PFFile.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ PF_ASSUME_NONNULL_BEGIN
3535
3636
@returns A new `PFFile`.
3737
*/
38-
+ (instancetype)fileWithData:(NSData *)data;
38+
+ (PF_NULLABLE instancetype)fileWithData:(NSData *)data;
3939

4040
/*!
4141
@abstract Creates a file with given data and name.
@@ -47,7 +47,7 @@ PF_ASSUME_NONNULL_BEGIN
4747
4848
@returns A new `PFFile` object.
4949
*/
50-
+ (instancetype)fileWithName:(PF_NULLABLE NSString *)name data:(NSData *)data;
50+
+ (PF_NULLABLE instancetype)fileWithName:(PF_NULLABLE NSString *)name data:(NSData *)data;
5151

5252
/*!
5353
@abstract Creates a file with the contents of another file.
@@ -61,7 +61,8 @@ PF_ASSUME_NONNULL_BEGIN
6161
6262
@returns A new `PFFile` instance.
6363
*/
64-
+ (instancetype)fileWithName:(PF_NULLABLE NSString *)name contentsAtPath:(NSString *)path;
64+
+ (PF_NULLABLE instancetype)fileWithName:(PF_NULLABLE NSString *)name
65+
contentsAtPath:(NSString *)path PF_SWIFT_UNAVAILABLE;
6566

6667
/*!
6768
@abstract Creates a file with the contents of another file.
@@ -75,7 +76,9 @@ PF_ASSUME_NONNULL_BEGIN
7576
7677
@returns A new `PFFile` instance or `nil` if the error occured.
7778
*/
78-
+ (instancetype)fileWithName:(PF_NULLABLE NSString *)name contentsAtPath:(NSString *)path error:(NSError **)error;
79+
+ (PF_NULLABLE instancetype)fileWithName:(PF_NULLABLE NSString *)name
80+
contentsAtPath:(NSString *)path
81+
error:(NSError **)error;
7982

8083
/*!
8184
@abstract Creates a file with given data, name and content type.
@@ -89,9 +92,9 @@ PF_ASSUME_NONNULL_BEGIN
8992
9093
@returns A new `PFFile` instance.
9194
*/
92-
+ (instancetype)fileWithName:(PF_NULLABLE NSString *)name
93-
data:(NSData *)data
94-
contentType:(PF_NULLABLE NSString *)contentType;
95+
+ (PF_NULLABLE instancetype)fileWithName:(PF_NULLABLE NSString *)name
96+
data:(NSData *)data
97+
contentType:(PF_NULLABLE NSString *)contentType PF_SWIFT_UNAVAILABLE;
9598

9699
/*!
97100
@abstract Creates a file with given data, name and content type.
@@ -106,10 +109,10 @@ PF_ASSUME_NONNULL_BEGIN
106109
107110
@returns A new `PFFile` instance or `nil` if the error occured.
108111
*/
109-
+ (instancetype)fileWithName:(PF_NULLABLE NSString *)name
110-
data:(NSData *)data
111-
contentType:(PF_NULLABLE NSString *)contentType
112-
error:(NSError **)error;
112+
+ (PF_NULLABLE instancetype)fileWithName:(PF_NULLABLE NSString *)name
113+
data:(NSData *)data
114+
contentType:(PF_NULLABLE NSString *)contentType
115+
error:(NSError **)error;
113116

114117
/*!
115118
@abstract Creates a file with given data and content type.

Parse/PFFile.m

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import "PFAsyncTaskQueue.h"
1818
#import "PFCommandResult.h"
1919
#import "PFCoreManager.h"
20+
#import "PFErrorUtilities.h"
2021
#import "PFFileController.h"
2122
#import "PFFileManager.h"
2223
#import "PFFileStagingController.h"
@@ -73,12 +74,26 @@ + (instancetype)fileWithName:(NSString *)name contentsAtPath:(NSString *)path {
7374
+ (instancetype)fileWithName:(NSString *)name contentsAtPath:(NSString *)path error:(NSError **)error {
7475
NSFileManager *fileManager = [NSFileManager defaultManager];
7576
BOOL directory = NO;
76-
PFParameterAssert([fileManager fileExistsAtPath:path isDirectory:&directory] && !directory,
77-
@"%@ is not a valid file path for a PFFile.", path);
7877

79-
NSDictionary *attributess = [fileManager attributesOfItemAtPath:path error:nil];
80-
unsigned long long length = [attributess[NSFileSize] unsignedLongValue];
81-
PFParameterAssert(length <= PFFileMaxFileSize, @"PFFile cannot be larger than %lli bytes", PFFileMaxFileSize);
78+
if (![fileManager fileExistsAtPath:path isDirectory:&directory] || directory) {
79+
NSString *message = [NSString stringWithFormat:@"Failed to create PFFile at path '%@': "
80+
"file does not exist.", path];
81+
*error = [NSError errorWithDomain:NSCocoaErrorDomain
82+
code:NSFileNoSuchFileError
83+
userInfo:@{ NSLocalizedDescriptionKey: message }];
84+
return nil;
85+
}
86+
87+
NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:nil];
88+
unsigned long long length = [attributes[NSFileSize] unsignedLongValue];
89+
if (length > PFFileMaxFileSize) {
90+
NSString *message = [NSString stringWithFormat:@"Failed to create PFFile at path '%@': "
91+
"file is larger than %lluMB.", path, (PFFileMaxFileSize >> 20)];
92+
*error = [NSError errorWithDomain:NSCocoaErrorDomain
93+
code:NSFileReadTooLargeError
94+
userInfo:@{ NSLocalizedDescriptionKey: message }];
95+
return nil;
96+
}
8297

8398
PFFile *file = [self fileWithName:name url:nil];
8499
if (![file _stageWithPath:path error:error]) {
@@ -100,8 +115,22 @@ + (instancetype)fileWithName:(NSString *)name
100115
data:(NSData *)data
101116
contentType:(NSString *)contentType
102117
error:(NSError **)error {
103-
PFParameterAssert([data length] <= PFFileMaxFileSize,
104-
@"PFFile cannot be larger than %llu bytes", PFFileMaxFileSize);
118+
if (!data) {
119+
NSString *message = @"Cannot create a PFFile with nil data.";
120+
*error = [NSError errorWithDomain:NSCocoaErrorDomain
121+
code:NSFileNoSuchFileError
122+
userInfo:@{ NSLocalizedDescriptionKey: message }];
123+
return nil;
124+
}
125+
126+
if ([data length] > PFFileMaxFileSize) {
127+
NSString *message = [NSString stringWithFormat:@"Failed to create PFFile with data: "
128+
"data is larger than %lluMB.", (PFFileMaxFileSize >> 20)];
129+
*error = [NSError errorWithDomain:NSCocoaErrorDomain
130+
code:NSFileReadTooLargeError
131+
userInfo:@{ NSLocalizedDescriptionKey: message }];
132+
return nil;
133+
}
105134

106135
PFFile *file = [[self alloc] initWithName:name urlString:nil mimeType:contentType];
107136
if (![file _stageWithData:data error:error]) {

Tests/Unit/FileUnitTests.m

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,30 @@ - (void)testContructors {
193193

194194
- (void)testConstructorWithTooLargeData {
195195
NSMutableData *data = [NSMutableData dataWithLength:(10 * 1048576 + 1)];
196-
PFAssertThrowsInvalidArgumentException([PFFile fileWithData:data]);
196+
197+
NSError *error = nil;
198+
PFFile *file = [PFFile fileWithName:@"testFile"
199+
data:data
200+
contentType:nil
201+
error:&error];
202+
203+
XCTAssertNil(file);
204+
XCTAssertEqualObjects(NSCocoaErrorDomain, error.domain);
205+
XCTAssertEqual(NSFileReadTooLargeError, error.code);
206+
}
207+
208+
- (void)testConstructorWithNilData {
209+
NSMutableData *data = nil;
210+
211+
NSError *error = nil;
212+
PFFile *file = [PFFile fileWithName:@"testFile"
213+
data:data
214+
contentType:nil
215+
error:&error];
216+
217+
XCTAssertNil(file);
218+
XCTAssertEqualObjects(NSCocoaErrorDomain, error.domain);
219+
XCTAssertEqual(NSFileNoSuchFileError, error.code);
197220
}
198221

199222
- (void)testUploading {

0 commit comments

Comments
 (0)