Skip to content

Commit 0492b05

Browse files
Use errors instead of exceptions when creating a PFFile.
Switching to errors provides a more consistent interface, as we should never use exceptions in objective-c except for unrecoverable errors. Fixes #338.
1 parent 36979f6 commit 0492b05

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-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:NSPOSIXErrorDomain
82+
code:ENOENT
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:NSPOSIXErrorDomain
93+
code:EFBIG
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:NSPOSIXErrorDomain
121+
code:EINVAL
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:NSPOSIXErrorDomain
130+
code:EFBIG
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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,16 @@ - (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(PFParseErrorDomain, error.domain);
205+
XCTAssertEqual(EFBIG, error.code);
197206
}
198207

199208
- (void)testUploading {

0 commit comments

Comments
 (0)