Skip to content

Commit 4123d66

Browse files
committed
Split NSError category into two to lessen Bridge dependence
1 parent 4249216 commit 4123d66

File tree

5 files changed

+213
-128
lines changed

5 files changed

+213
-128
lines changed

APCAppCore/APCAppCore.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@
322322
CFFDEDFA1A95734000B25581 /* APCSetupTableViewCell.h in Headers */ = {isa = PBXBuildFile; fileRef = CFFDEDC01A95734000B25581 /* APCSetupTableViewCell.h */; settings = {ATTRIBUTES = (Public, ); }; };
323323
CFFDEDFB1A95734000B25581 /* APCSetupTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = CFFDEDC11A95734000B25581 /* APCSetupTableViewCell.m */; };
324324
CFFDEDFC1A95734000B25581 /* APCSetupTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = CFFDEDC21A95734000B25581 /* APCSetupTableViewCell.xib */; };
325+
EE4B95251AF82BA6000097C7 /* NSError+Bridge.h in Headers */ = {isa = PBXBuildFile; fileRef = EE4B95231AF82BA6000097C7 /* NSError+Bridge.h */; };
326+
EE4B95261AF82BA6000097C7 /* NSError+Bridge.m in Sources */ = {isa = PBXBuildFile; fileRef = EE4B95241AF82BA6000097C7 /* NSError+Bridge.m */; };
325327
F50738C01A682E12004CF100 /* APCDateRange.h in Headers */ = {isa = PBXBuildFile; fileRef = F50738BE1A682E12004CF100 /* APCDateRange.h */; settings = {ATTRIBUTES = (Public, ); }; };
326328
F50738C11A682E12004CF100 /* APCDateRange.m in Sources */ = {isa = PBXBuildFile; fileRef = F50738BF1A682E12004CF100 /* APCDateRange.m */; };
327329
F5306CCD1A8BE7F600732E60 /* ORKQuestionResult+APCHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = F5306CCB1A8BE7F600732E60 /* ORKQuestionResult+APCHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -1005,6 +1007,8 @@
10051007
CFFDEDC01A95734000B25581 /* APCSetupTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APCSetupTableViewCell.h; sourceTree = "<group>"; };
10061008
CFFDEDC11A95734000B25581 /* APCSetupTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APCSetupTableViewCell.m; sourceTree = "<group>"; };
10071009
CFFDEDC21A95734000B25581 /* APCSetupTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = APCSetupTableViewCell.xib; sourceTree = "<group>"; };
1010+
EE4B95231AF82BA6000097C7 /* NSError+Bridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError+Bridge.h"; sourceTree = "<group>"; };
1011+
EE4B95241AF82BA6000097C7 /* NSError+Bridge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSError+Bridge.m"; sourceTree = "<group>"; };
10081012
F50738BE1A682E12004CF100 /* APCDateRange.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APCDateRange.h; sourceTree = "<group>"; };
10091013
F50738BF1A682E12004CF100 /* APCDateRange.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = APCDateRange.m; sourceTree = "<group>"; };
10101014
F5179B2919D09128001DCCB7 /* APCAppCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = APCAppCore.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -2189,6 +2193,8 @@
21892193
F5B9474D1A73272C0034C522 /* NSDictionary+APCAdditions.m */,
21902194
F5B9474E1A73272C0034C522 /* NSError+APCAdditions.h */,
21912195
F5B9474F1A73272C0034C522 /* NSError+APCAdditions.m */,
2196+
EE4B95231AF82BA6000097C7 /* NSError+Bridge.h */,
2197+
EE4B95241AF82BA6000097C7 /* NSError+Bridge.m */,
21922198
F5B947501A73272C0034C522 /* NSManagedObject+APCHelper.h */,
21932199
F5B947511A73272C0034C522 /* NSManagedObject+APCHelper.m */,
21942200
F5B947521A73272C0034C522 /* NSObject+Helper.h */,
@@ -2983,6 +2989,7 @@
29832989
CF7945861AA7AEC70019160F /* APCFrequencyEverydayTableViewCell.h in Headers */,
29842990
F5F12A7B1A2F78490015982C /* APCCircleView.h in Headers */,
29852991
F5F129FE1A2F78490015982C /* APCSchedule.h in Headers */,
2992+
EE4B95251AF82BA6000097C7 /* NSError+Bridge.h in Headers */,
29862993
0875C3D81A797A7B00CE50FB /* APCButton.h in Headers */,
29872994
5B9B36A71A95D9C900389F42 /* APCActivitiesBasicTableViewCell.h in Headers */,
29882995
3650C65B1AA29BB50075C935 /* APCCatastrophicErrorViewController.h in Headers */,
@@ -3466,6 +3473,7 @@
34663473
3627D34D1A9A7517006B02E8 /* APCMedTrackerPossibleDosage.m in Sources */,
34673474
5B9B36A41A95D9B500389F42 /* APCActivitiesTintedTableViewCell.m in Sources */,
34683475
F5F12AC61A2F78490015982C /* APCLearnStudyDetailsViewController.m in Sources */,
3476+
EE4B95261AF82BA6000097C7 /* NSError+Bridge.m in Sources */,
34693477
F5B947EA1A73272C0034C522 /* APCTasksReminderManager.m in Sources */,
34703478
F5B948051A73272C0034C522 /* APCPointSelector.m in Sources */,
34713479
F5F12AAD1A2F78490015982C /* APCSignUpGeneralInfoViewController.m in Sources */,

APCAppCore/APCAppCore/Library/Categories/NSError+APCAdditions.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@
3333

3434
#import <Foundation/Foundation.h>
3535

36+
FOUNDATION_EXPORT NSString * const kServerBusy;
37+
FOUNDATION_EXPORT NSString * const kUnexpectConditionMessage;
38+
FOUNDATION_EXPORT NSString * const kNotConnectedMessage;
39+
FOUNDATION_EXPORT NSString * const kServerMaintanenceMessage;
40+
FOUNDATION_EXPORT NSString * const kAccountAlreadyExists;
41+
FOUNDATION_EXPORT NSString * const kAccountDoesNotExists;
42+
FOUNDATION_EXPORT NSString * const kBadEmailAddress;
43+
FOUNDATION_EXPORT NSString * const kBadPasswordAddress;
44+
FOUNDATION_EXPORT NSString * const kNotReachableMessage;
45+
FOUNDATION_EXPORT NSString * const kInvalidEmailAddressOrPassword;
46+
3647
@interface NSError (APCAdditions)
3748

3849
/*********************************************************************************/

APCAppCore/APCAppCore/Library/Categories/NSError+APCAdditions.m

Lines changed: 37 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,19 @@
3131
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232
//
3333

34-
#import "APCAppCore.h"
35-
36-
static NSString* kServerBusy = @"Thank you for your interest in this study. We are working hard to process the large volume of interest, and should be back up momentarily. Please try again soon.";
37-
static NSString* kUnexpectConditionMessage = @"An unexpected network condition has occurred. Please try again soon.";
38-
static NSString* kNotConnectedMessage = @"You are currently not connected to the Internet. Please try again when you are connected to a network.";
39-
static NSString* kServerMaintanenceMessage = @"The study server is currently undergoing maintanence. Please try again soon.";
40-
static NSString* kAccountAlreadyExists = @"An account has already been created for this email address. Please use a different email address, or sign in using the \"already participating\" link at the bottom of the Welcome page.";
41-
static NSString* kAccountDoesNotExists = @"There is no account registered for this email address.";
42-
static NSString* kBadEmailAddress = @"The email address submitted is not a valid email address. Please correct the email address and try again.";
43-
static NSString* kBadPasswordAddress = @"The password you have entered is not a valid password. Please try again.";
44-
static NSString* kNotReachableMessage = @"We are currently not able to reach the study server. Please retry in a few moments.";
45-
static NSString* kInvalidEmailAddressOrPassword = @"Entered email address or password is not valid. Please correct the email address or password and try again.";
46-
static NSString* kSageMessageKey = @"message";
47-
static NSString* kSageErrorsKey = @"errors";
48-
static NSString* kSageErrorPasswordKey = @"password";
49-
static NSString* kSageErrorEmailKey = @"email";
50-
static NSString* kSageInvalidUsernameOrPassword = @"Invalid username or password.";
34+
#import "NSError+APCAdditions.h"
35+
#import "APCLog.h"
36+
37+
NSString * const kServerBusy = @"Thank you for your interest in this study. We are working hard to process the large volume of interest, and should be back up momentarily. Please try again soon.";
38+
NSString * const kUnexpectConditionMessage = @"An unexpected network condition has occurred. Please try again soon.";
39+
NSString * const kNotConnectedMessage = @"You are currently not connected to the Internet. Please try again when you are connected to a network.";
40+
NSString * const kServerMaintanenceMessage = @"The study server is currently undergoing maintanence. Please try again soon.";
41+
NSString * const kAccountAlreadyExists = @"An account has already been created for this email address. Please use a different email address, or sign in using the \"already participating\" link at the bottom of the Welcome page.";
42+
NSString * const kAccountDoesNotExists = @"There is no account registered for this email address.";
43+
NSString * const kBadEmailAddress = @"The email address submitted is not a valid email address. Please correct the email address and try again.";
44+
NSString * const kBadPasswordAddress = @"The password you have entered is not a valid password. Please try again.";
45+
NSString * const kNotReachableMessage = @"We are currently not able to reach the study server. Please retry in a few moments.";
46+
NSString * const kInvalidEmailAddressOrPassword = @"Entered email address or password is not valid. Please correct the email address or password and try again.";
5147

5248
static NSString * const oneTab = @" ";
5349

@@ -62,11 +58,6 @@ - (NSString*)checkMessageForNonUserTerms:(NSString*)message
6258
{
6359
return NSLocalizedString(@"An unknown error occurred", nil);
6460
}
65-
else
66-
{
67-
return message;
68-
}
69-
7061
return message;
7162
}
7263

@@ -82,117 +73,44 @@ - (void)handle
8273
}
8374

8475

85-
- (NSString*)bridgeErrorMessage
86-
{
87-
NSString* message;
88-
id code = self.userInfo[SBB_ORIGINAL_ERROR_KEY];
89-
90-
if ([code isKindOfClass:[NSError class]])
91-
{
92-
NSError* e = (NSError*)code;
93-
94-
if (e.code == kCFURLErrorNotConnectedToInternet)
95-
{
96-
message = NSLocalizedString(kNotConnectedMessage, nil);
97-
}
98-
else
99-
{
100-
APCLogError(@"Network error: %@", code);
101-
}
102-
}
103-
else if (self.code == 400)
104-
{
105-
// There are several messages that need to be displayed within the 400
106-
// Extract the internal message then act appropriately.
107-
NSDictionary * errors = [code valueForKey: kSageErrorsKey];
108-
if([errors valueForKey: kSageErrorEmailKey])
109-
{
110-
message = NSLocalizedString(kBadEmailAddress, nil);
111-
}
112-
else if([errors valueForKey: kSageErrorPasswordKey])
113-
{
114-
message = NSLocalizedString(kBadPasswordAddress, nil);
115-
} else
116-
{
117-
message = NSLocalizedString(kInvalidEmailAddressOrPassword, nil);
118-
}
119-
120-
}
121-
else if (self.code == 409)
122-
{
123-
message = NSLocalizedString(kAccountAlreadyExists, nil);
124-
}
125-
else if (self.code == 404)
126-
{
127-
message = NSLocalizedString(kAccountDoesNotExists, nil);
128-
}
129-
else if ([code isEqual:@(503)] || self.code == 503)
130-
{
131-
message = NSLocalizedString(kServerBusy, nil);
132-
}
133-
else if ([code isEqual: @(kSBBInternetNotConnected)])
134-
{
135-
message = NSLocalizedString(kNotConnectedMessage, nil);
136-
}
137-
else if ([code isEqual:@(kSBBServerNotReachable)])
138-
{
139-
message = NSLocalizedString(kNotReachableMessage, nil);
140-
}
141-
else if ([code isEqual:@(kSBBServerUnderMaintenance)])
142-
{
143-
message = NSLocalizedString(kServerMaintanenceMessage, nil);
144-
}
145-
else
146-
{
147-
message = NSLocalizedString(kUnexpectConditionMessage, nil);
148-
}
149-
150-
return message;
151-
}
152-
153-
154-
- (NSString*)networkErrorMessage
76+
- (NSString *)networkErrorMessage
15577
{
156-
NSString* message;
78+
NSString *message;
15779

158-
if (self.code == 409)
159-
{
80+
if (self.code == 409) {
16081
message = NSLocalizedString(kAccountAlreadyExists, nil);
16182
}
162-
else if (self.code == 404)
163-
{
83+
else if (self.code == 404) {
16484
message = NSLocalizedString(kAccountDoesNotExists, nil);
16585
}
166-
else if (self.code >= 500 && self.code < 600)
167-
{
86+
else if (self.code >= 500 && self.code < 600) {
16887
message = NSLocalizedString(kServerBusy, nil);
16988
}
170-
else if (self.code == kCFURLErrorDNSLookupFailed || self.code == kCFURLErrorInternationalRoamingOff)
171-
{
89+
else if (self.code == kCFURLErrorDNSLookupFailed || self.code == kCFURLErrorInternationalRoamingOff) {
17290
message = NSLocalizedString(kNotConnectedMessage, nil);
17391
}
174-
else
175-
{
92+
else {
17693
message = NSLocalizedString(kUnexpectConditionMessage, nil);
17794
}
17895

17996
return message;
18097
}
18198

18299

183-
- (NSString*)message
100+
- (NSString *)message
184101
{
185-
NSString* message = kUnexpectConditionMessage;
102+
NSString *message = kUnexpectConditionMessage;
186103

187-
if ([self.domain isEqualToString:(__bridge NSString*)kCFErrorDomainCFNetwork])
188-
{
104+
if ([self.domain isEqualToString:(__bridge NSString*)kCFErrorDomainCFNetwork]) {
189105
message = [self networkErrorMessage];
190106
}
191-
else if ([self.domain isEqualToString:SBB_ERROR_DOMAIN])
192-
{
193-
message = [self bridgeErrorMessage];
107+
#pragma clang diagnostic push
108+
#pragma clang diagnostic ignored "-Wundeclared-selector"
109+
else if ([self respondsToSelector:@selector(bridgeErrorMessage)]) {
110+
message = [self performSelector:@selector(bridgeErrorMessage)];
194111
}
195-
112+
#pragma clang diagnostic pop
113+
196114
return [NSString stringWithFormat:@"\n%@\n\nError code: %@", [self checkMessageForNonUserTerms:message], @(self.code)];
197115
}
198116

@@ -315,41 +233,34 @@ + (NSError *) errorWithCode: (NSInteger) code
315233
#pragma mark - Friendly printouts
316234
// ---------------------------------------------------------
317235

318-
- (NSString *) friendlyFormattedString
236+
- (NSString *)friendlyFormattedString
319237
{
320238
return [self friendlyFormattedStringAtLevel: 0];
321239
}
322240

323-
- (NSString *) friendlyFormattedStringAtLevel: (NSUInteger) tabLevel
241+
- (NSString *)friendlyFormattedStringAtLevel:(NSUInteger)tabLevel
324242
{
325243
NSMutableString *output = [NSMutableString new];
326-
327244
NSString *tab = [@"" stringByPaddingToLength: tabLevel * oneTab.length
328245
withString: oneTab
329246
startingAtIndex: 0];
330247

331248
NSString *tabForNestedObjects = [NSString stringWithFormat: @"\n%@", tab];
332-
333249
NSString *domain = self.domain.length > 0 ? self.domain : @"(none)";
334250

335251
[output appendFormat: @"%@Code: %@\n", tab, @(self.code)];
336252
[output appendFormat: @"%@Domain: %@\n", tab, domain];
337253

338-
if (self.userInfo.count > 0)
339-
{
340-
for (NSString *key in [self.userInfo.allKeys sortedArrayUsingSelector: @selector (compare:)])
341-
{
254+
if (self.userInfo.count > 0) {
255+
for (NSString *key in [self.userInfo.allKeys sortedArrayUsingSelector: @selector (compare:)]) {
342256
id value = self.userInfo [key];
343257
NSString *valueString = nil;
344258

345-
if ([value isKindOfClass: [NSError class]])
346-
{
259+
if ([value isKindOfClass: [NSError class]]) {
347260
valueString = [value friendlyFormattedStringAtLevel: tabLevel + 1];
348261
[output appendFormat: @"%@%@:\n%@", tab, key, valueString];
349262
}
350-
351-
else
352-
{
263+
else {
353264
valueString = [NSString stringWithFormat: @"%@", value];
354265
valueString = [valueString stringByReplacingOccurrencesOfString: @"\\n" withString: @"\n"];
355266
valueString = [valueString stringByReplacingOccurrencesOfString: @"\\\"" withString: @"\""];
@@ -359,12 +270,10 @@ - (NSString *) friendlyFormattedStringAtLevel: (NSUInteger) tabLevel
359270
}
360271
}
361272

362-
if (tabLevel == 0)
363-
{
273+
if (tabLevel == 0) {
364274
[output insertString: @"An error occurred. Available info:\n----- ERROR INFO -----\n" atIndex: 0];
365275

366-
if ([output characterAtIndex: output.length - 1] != '\n')
367-
{
276+
if ([output characterAtIndex: output.length - 1] != '\n') {
368277
[output appendString: @"\n"];
369278
}
370279

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// NSError+Bridge.h
3+
// AppCore
4+
//
5+
// Copyright (c) 2015 Boston Children's Hospital. All rights reserved.
6+
//
7+
// Redistribution and use in source and binary forms, with or without modification,
8+
// are permitted provided that the following conditions are met:
9+
//
10+
// 1. Redistributions of source code must retain the above copyright notice, this
11+
// list of conditions and the following disclaimer.
12+
//
13+
// 2. Redistributions in binary form must reproduce the above copyright notice,
14+
// this list of conditions and the following disclaimer in the documentation and/or
15+
// other materials provided with the distribution.
16+
//
17+
// 3. Neither the name of the copyright holder(s) nor the names of any contributors
18+
// may be used to endorse or promote products derived from this software without
19+
// specific prior written permission. No license is granted to the trademarks of
20+
// the copyright holders even if such marks are included in this software.
21+
//
22+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
26+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28+
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
//
33+
34+
#import "NSError+APCAdditions.h"
35+
36+
@interface NSError (Bridge)
37+
38+
@end

0 commit comments

Comments
 (0)