Skip to content

Commit 241e39c

Browse files
committed
Change a few errors to the new functions
1 parent 743c531 commit 241e39c

File tree

4 files changed

+110
-43
lines changed

4 files changed

+110
-43
lines changed

Classes/Controllers/PBGitRepositoryDocument.m

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,13 @@ - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSEr
2525
{
2626
if (![PBGitBinary path])
2727
{
28-
if (outError) {
29-
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:[PBGitBinary notFoundError]
30-
forKey:NSLocalizedRecoverySuggestionErrorKey];
31-
*outError = [NSError errorWithDomain:PBGitXErrorDomain code:0 userInfo:userInfo];
32-
}
33-
return NO;
28+
return PBReturnError(outError, @"Unable to find git", [PBGitBinary notFoundError], nil);
3429
}
3530

3631
BOOL isDirectory = FALSE;
3732
[[NSFileManager defaultManager] fileExistsAtPath:[absoluteURL path] isDirectory:&isDirectory];
3833
if (!isDirectory) {
39-
if (outError) {
40-
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:@"Reading files is not supported."
41-
forKey:NSLocalizedRecoverySuggestionErrorKey];
42-
*outError = [NSError errorWithDomain:PBGitXErrorDomain code:0 userInfo:userInfo];
43-
}
44-
return NO;
34+
return PBReturnError(outError, @"Unable to read files", @"Reading files is not supported", nil);
4535
}
4636

4737
_repository = [[PBGitRepository alloc] initWithURL:absoluteURL error:outError];

Classes/Util/PBError.h

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,78 @@ NS_ASSUME_NONNULL_BEGIN
2424
extern NSString * const PBGitXErrorDomain;
2525

2626
@interface NSError (PBError)
27+
28+
/**
29+
* GitX helper for error creation.
30+
*
31+
* @see pb_errorWithDescription:failureReason:underlyingError:userInfo:
32+
*/
2733
+ (NSError *)pb_errorWithDescription:(NSString *)description failureReason:(NSString *)failureReason;
34+
35+
/**
36+
* GitX helper for error creation, with user info.
37+
*
38+
* @see pb_errorWithDescription:failureReason:underlyingError:userInfo:
39+
*/
2840
+ (NSError *)pb_errorWithDescription:(NSString *)description failureReason:(NSString *)failureReason userInfo:(nullable NSDictionary *)userInfo;
41+
42+
/**
43+
* GitX helper for error creation, with underlying error.
44+
*
45+
* @see pb_errorWithDescription:failureReason:underlyingError:userInfo:
46+
*/
2947
+ (NSError *)pb_errorWithDescription:(NSString *)description failureReason:(NSString *)failureReason underlyingError:(nullable NSError *)underError;
48+
49+
/** GitX helper for error creation, with underlying error and user info.
50+
*
51+
* This uses a @p 0 error code and the @p PBGitXErrorDomain domain as defaults.
52+
*
53+
* @notes
54+
* The values set as @p description, @p failureReason and @p underlyingError are
55+
* used in priority over those in the @p userInfo dictionary (if any).
56+
*
57+
* @param description A quick description of the error.
58+
* @param failureReason A more verbose explanation.
59+
* @param underlyingError An error to set as the underlying error.
60+
* @param userInfo A dictionary to use as the error userInfo.
61+
*
62+
* @return A newly initialized NSError instance.
63+
*/
3064
+ (NSError *)pb_errorWithDescription:(NSString *)description failureReason:(NSString *)failureReason underlyingError:(nullable NSError *)underError userInfo:(nullable NSDictionary *)userInfo;
3165
@end
3266

33-
BOOL PBReturnError(NSError **error, NSString *description, NSString *failureReason);
67+
/**
68+
* Easily handle NSError double-pointers.
69+
*
70+
* @param error The error parameter to fill.
71+
* @param description A quick description of the error.
72+
* @param failureReason A more verbose explanation.
73+
* @param underlyingError An error to set as the underlying error.
74+
*
75+
* @return NO.
76+
*/
77+
BOOL PBReturnError(NSError **error, NSString *description, NSString *failureReason, NSError * __nullable underlyingError);
78+
79+
/**
80+
* Helper function for easily handling NSError double-pointers.
81+
*
82+
* @param error The error parameter to fill.
83+
* @param description A quick description of the error.
84+
* @param failureReason A more verbose explanation.
85+
* @param userInfo A dictionary to use as the error userInfo.
86+
*
87+
* @return NO.
88+
*/
3489
BOOL PBReturnErrorWithUserInfo(NSError **error, NSString *description, NSString *failureReason, NSDictionary * _Nullable userInfo);
90+
91+
/**
92+
* Helper function for easily handling NSError double-pointers.
93+
*
94+
* @param error The error parameter to fill.
95+
* @param builder A block responsible to create a valid NSError object.
96+
*
97+
* @return NO.
98+
*/
3599
BOOL PBReturnErrorWithBuilder(NSError **error, NSError * (^errorBuilder)(void));
36100

37101
NS_ASSUME_NONNULL_END

Classes/Util/PBError.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ + (NSError *)pb_errorWithDescription:(NSString *)description failureReason:(NSSt
5959
@end
6060

6161

62-
BOOL PBReturnError(NSError **error, NSString *description, NSString *failureReason) {
63-
return PBReturnErrorWithUserInfo(error, description, failureReason, nil);
62+
BOOL PBReturnError(NSError **error, NSString *description, NSString *failureReason, NSError *underlyingError) {
63+
if (error) {
64+
*error = [NSError pb_errorWithDescription:description failureReason:failureReason underlyingError:underlyingError];
65+
}
66+
return NO;
6467
}
6568

6669
BOOL PBReturnErrorWithUserInfo(NSError **error, NSString *description, NSString *failureReason, NSDictionary *userInfo) {

Classes/git/PBGitRepository.m

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ - (id)initWithURL:(NSURL *)repositoryURL error:(NSError **)error
6868
_gtRepo = [GTRepository repositoryWithURL:repoURL error:&gtError];
6969
if (!_gtRepo) {
7070
if (error) {
71-
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
72-
[NSString stringWithFormat:@"%@ does not appear to be a git repository.", [repositoryURL path]], NSLocalizedRecoverySuggestionErrorKey,
73-
gtError, NSUnderlyingErrorKey,
74-
nil];
75-
*error = [NSError errorWithDomain:PBGitXErrorDomain code:0 userInfo:userInfo];
71+
*error = [NSError pb_errorWithDescription:NSLocalizedString(@"Repository initialization failed", @"")
72+
failureReason:[NSString stringWithFormat:NSLocalizedString(@"%@ does not appear to be a git repository.", @""), repositoryURL.path]
73+
underlyingError:gtError];
7674
}
7775
return nil;
7876
}
@@ -644,32 +642,44 @@ - (PBGitRef *) remoteRefForBranch:(PBGitRef *)branch error:(NSError **)error
644642
return [branch remoteRef];
645643
}
646644

647-
NSString *branchRef = branch.ref;
648-
if (branchRef) {
649-
NSError *branchError = nil;
650-
BOOL lookupSuccess = NO;
651-
GTBranch *gtBranch = [self.gtRepo lookUpBranchWithName:branch.branchName type:GTBranchTypeLocal success:&lookupSuccess error:&branchError];
652-
if (gtBranch && lookupSuccess) {
653-
NSError *trackingError = nil;
654-
BOOL trackingSuccess = NO;
655-
GTBranch *trackingBranch = [gtBranch trackingBranchWithError:&trackingError success:&trackingSuccess];
656-
if (trackingBranch && trackingSuccess) {
657-
NSString *trackingBranchRefName = trackingBranch.reference.name;
658-
PBGitRef *trackingBranchRef = [PBGitRef refFromString:trackingBranchRefName];
659-
return trackingBranchRef;
660-
}
661-
}
645+
NSError *gtError = nil;
646+
BOOL success = NO;
647+
NSAssert(branch.ref != nil, @"Unexpected nil ref");
648+
649+
GTBranch *gtBranch = [self.gtRepo lookUpBranchWithName:branch.branchName type:GTBranchTypeLocal success:&success error:&gtError];
650+
if (!success) {
651+
NSString *failure = [NSString stringWithFormat:NSLocalizedString(@"There was an error looking up the branch \"%@\"", @""), branch.shortName];
652+
PBReturnError(error, NSLocalizedString(@"Branch lookup failed", @""), failure, gtError);
653+
return nil;
654+
}
655+
if (!gtBranch) {
656+
NSString *failure = [NSString stringWithFormat:NSLocalizedString(@"There doesn't seem to be a branch named \"%@\"", @""), branch.shortName];
657+
PBReturnError(error, NSLocalizedString(@"Branch lookup failed", @""), failure, gtError);
658+
return nil;
662659
}
663660

664-
if (error != NULL) {
665-
NSString *info = [NSString stringWithFormat:@"There is no remote configured for the %@ '%@'.\n\nPlease select a branch from the popup menu, which has a corresponding remote tracking branch set up.\n\nYou can also use a contextual menu to choose a branch by right clicking on its label in the commit history list.", [branch refishType], [branch shortName]];
666-
*error = [NSError errorWithDomain:PBGitXErrorDomain code:0
667-
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
668-
@"No remote configured for branch", NSLocalizedDescriptionKey,
669-
info, NSLocalizedRecoverySuggestionErrorKey,
670-
nil]];
661+
GTBranch *trackingBranch = [gtBranch trackingBranchWithError:&gtError success:&success];
662+
if (!success) {
663+
NSString *failure = [NSString stringWithFormat:NSLocalizedString(@"There was an error finding the tracking branch of branch \"%@\"", @""), branch.shortName];
664+
PBReturnError(error, NSLocalizedString(@"Branch lookup failed", @""), failure, gtError);
665+
return nil;
671666
}
672-
return nil;
667+
if (!trackingBranch) {
668+
PBReturnErrorWithBuilder(error, ^{
669+
NSString *info = [NSString stringWithFormat:@"There is no remote configured for branch \"%@\".", branch.shortName];
670+
NSString *recovery = NSLocalizedString(@"Please select a branch from the popup menu, which has a corresponding remote tracking branch set up.\n\nYou can also use a contextual menu to choose a branch by right clicking on its label in the commit history list.", @"");
671+
672+
return [NSError pb_errorWithDescription:NSLocalizedString(@"No remote configured for branch", @"")
673+
failureReason:info
674+
underlyingError:gtError
675+
userInfo:@{NSLocalizedRecoverySuggestionErrorKey: recovery}];
676+
});
677+
return nil;
678+
}
679+
680+
NSString *trackingBranchRefName = trackingBranch.reference.name;
681+
PBGitRef *trackingBranchRef = [PBGitRef refFromString:trackingBranchRefName];
682+
return trackingBranchRef;
673683
}
674684

675685
- (NSString *) infoForRemote:(NSString *)remoteName

0 commit comments

Comments
 (0)