Skip to content

Commit 49b4043

Browse files
committed
Improve handling of Swift errors in mmm_description
Previously any underlying errors from Swift would be automatically converted into NSError and become useless, now we are using their descriptions at least.
1 parent 4aee0d6 commit 49b4043

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

MMMCommonCore.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Pod::Spec.new do |s|
77

88
s.name = "MMMCommonCore"
9-
s.version = "1.9.0"
9+
s.version = "1.10.0"
1010
s.summary = "Small bits and pieces reused in many pods from MMMTemple"
1111
s.description = s.summary
1212
s.homepage = "https://github.com/mediamonks/#{s.name}"

Sources/MMMCommonCoreObjC/MMMCommonCore.m

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,26 +207,32 @@ - (id)mmm_stripNSNull {
207207
@implementation NSError (MMMTemple)
208208

209209
- (NSString *)mmm_description {
210-
211210
NSMutableString *result = [[NSMutableString alloc] init];
212-
213211
NSError *e = self;
214212
// Note that Swift errors returning `nil` for underlyingError might end up with NSNull there.
215213
while (e && ![e isKindOfClass:[NSNull class]]) {
216-
217214
if ([result length] > 0)
218215
[result appendString:@" > "];
216+
[result appendString:[e mmm_nonRecursiveDescription]];
217+
e = e.userInfo[NSUnderlyingErrorKey];
218+
}
219+
return result;
220+
}
219221

222+
- (NSString *)mmm_nonRecursiveDescription {
223+
if (self.class == NSClassFromString(@"__SwiftNativeNSError")) {
224+
// Treating underlying Swift errors as NSError is normally useless and it's better to fall back to their descriptions instead.
225+
// (I could check for `self.class != NSError.class` here to make it less dependent on the actual system class name,
226+
// but that would include rare subclasses like `NSURLError`; I could also check for "Swift" in the class name, but that still
227+
// would not guarantee to cover future changes in the standard library anyway.)
228+
return [self description];
229+
} else {
220230
// Treating the -1 error code as "other" kind of error, where only the message matters for diagnostics.
221-
if (e.code != -1)
222-
[result appendString:[NSString stringWithFormat:@"%@ (%@#%ld)", e.localizedDescription, e.domain, (long)e.code]];
231+
if (self.code != -1)
232+
return [NSString stringWithFormat:@"%@ (%@#%ld)", self.localizedDescription, self.domain, (long)self.code];
223233
else
224-
[result appendString:[NSString stringWithFormat:@"%@ (%@)", e.localizedDescription, e.domain]];
225-
226-
e = e.userInfo[NSUnderlyingErrorKey];
234+
return [NSString stringWithFormat:@"%@ (%@)", self.localizedDescription, self.domain];
227235
}
228-
229-
return result;
230236
}
231237

232238
- (NSError *)mmm_underlyingError {

0 commit comments

Comments
 (0)