Skip to content

Commit

Permalink
Try to improve mmm_description for cases where NSError is transformed
Browse files Browse the repository at this point in the history
NSError crossing concurrency domains (thrown from an async function) appears to
be replaced with a native implementation with different runtime type affecting
the current logic of mmm_description which would use regular description for
Swift-native errors. A workaround for now is to check for code not being -1.
  • Loading branch information
aleh committed Jun 19, 2024
1 parent a05d527 commit 4368736
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion MMMCommonCore.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Pod::Spec.new do |s|

s.name = "MMMCommonCore"
s.version = "1.16.1"
s.version = "1.16.2"
s.summary = "Small bits and pieces reused in many pods from MMMTemple"
s.description = s.summary
s.homepage = "https://github.com/mediamonks/#{s.name}"
Expand Down
12 changes: 5 additions & 7 deletions Sources/MMMCommonCore/CommonCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,14 @@ private struct PairsIterator<Iterator: IteratorProtocol>: IteratorProtocol {
// MARK: -

extension Error {

/// Better string representation for `Error` and `NSError`s.
///
/// This is a Swift version of `mmm_description` that allows to avoid casting to `NSError`
/// and which falls back `String(describing:)` for "not really" `NSError`s to avoid meaningless
/// "The operation couldn't be completed" messages.
public var mmm_description: String {
if type(of: self) is NSError.Type {
return (self as NSError).mmm_description()
} else {
return String(describing: self)
}
(self as NSError).mmm_description()
}
}

Expand Down Expand Up @@ -193,12 +190,13 @@ public func MMMTypeName(_ value: Any) -> String {
}

extension NSError {

/// Initialize using the given value's type name as a domain string.
public convenience init(domain: Any, message: String, code: Int = -1, underlyingError: Error? = nil) {
var userInfo = [String: Any]()
userInfo[NSLocalizedDescriptionKey] = message
if let underlyingError = underlyingError as NSError? {
userInfo[NSUnderlyingErrorKey] = underlyingError
if let underlyingError {
userInfo[NSUnderlyingErrorKey] = underlyingError as NSError
}
let domain = (domain as? String) ?? MMMTypeName(domain)
self.init(domain: domain, code: code, userInfo: userInfo)
Expand Down
4 changes: 3 additions & 1 deletion Sources/MMMCommonCoreObjC/MMMCommonCore.m
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,13 @@ - (NSString *)mmm_description {
}

- (NSString *)mmm_nonRecursiveDescription {
if (self.class == NSClassFromString(@"__SwiftNativeNSError")) {
if (self.class == NSClassFromString(@"__SwiftNativeNSError") && self.code != -1) {
// Treating underlying Swift errors as NSError is normally useless and it's better to fall back to their descriptions instead.
// (I could check for `self.class != NSError.class` here to make it less dependent on the actual system class name,
// but that would include rare subclasses like `NSURLError`; I could also check for "Swift" in the class name, but that still
// would not guarantee to cover future changes in the standard library anyway.)
// Update: now we have to check for code not being -1 as `NSError` are now translated into Swift ones when
// crossing concurrency domains.
return [self description];
} else {
// Treating the -1 error code as "other" kind of error, where only the message matters for diagnostics.
Expand Down

0 comments on commit 4368736

Please sign in to comment.