-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add type-erased AnyError
for generic contexts
#198
Changes from 3 commits
7b1d6ab
a7d0cdc
fd89d0e
3cafe3e
1a12328
aac425d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,10 +108,26 @@ public enum Result<T, Error: Swift.Error>: ResultProtocol, CustomStringConvertib | |
|
||
// MARK: - Derive result from failable closure | ||
|
||
public func materialize<T>(_ f: () throws -> T) -> Result<T, AnyError> { | ||
return materialize(try f()) | ||
} | ||
|
||
public func materialize<T>(_ f: @autoclosure () throws -> T) -> Result<T, AnyError> { | ||
do { | ||
return .success(try f()) | ||
} catch let error as AnyError { | ||
return .failure(error) | ||
} catch { | ||
return .failure(AnyError(error)) | ||
} | ||
} | ||
|
||
@available(*, deprecated, message: "Use the overload which returns `Result<T, AnyError>` instead") | ||
public func materialize<T>(_ f: () throws -> T) -> Result<T, NSError> { | ||
return materialize(try f()) | ||
} | ||
|
||
@available(*, deprecated, message: "Use the overload which returns `Result<T, AnyError>` instead") | ||
public func materialize<T>(_ f: @autoclosure () throws -> T) -> Result<T, NSError> { | ||
do { | ||
return .success(try f()) | ||
|
@@ -160,7 +176,7 @@ extension NSError: ErrorProtocolConvertible { | |
} | ||
} | ||
|
||
// MARK: - | ||
// MARK: - Errors | ||
|
||
/// An “error” that is impossible to construct. | ||
/// | ||
|
@@ -169,6 +185,33 @@ extension NSError: ErrorProtocolConvertible { | |
/// contains an `Int`eger and is guaranteed never to be a `failure`. | ||
public enum NoError: Swift.Error { } | ||
|
||
/// A type-erased error which wraps an arbitrary error instance. This should be | ||
/// useful for generic contexts. | ||
public struct AnyError: Swift.Error, ErrorProtocolConvertible, Equatable, CustomStringConvertible { | ||
/// The underlying error. | ||
public let error: Swift.Error | ||
|
||
public init(_ error: Swift.Error) { | ||
self.error = error | ||
} | ||
|
||
public static func error(from error: Error) -> AnyError { | ||
if let anyError = error as? AnyError { | ||
return anyError | ||
} | ||
return AnyError(error) | ||
} | ||
|
||
public static func ==(lhs: AnyError, rhs: AnyError) -> Bool { | ||
return lhs.error._code == rhs.error._code | ||
&& lhs.error._domain == rhs.error._domain | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I withdraw my comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quick question: Are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not confident. It should be okay to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
+1. I also think removing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 3cafe3e. |
||
} | ||
|
||
public var description: String { | ||
return String(describing: error) | ||
} | ||
} | ||
|
||
// MARK: - migration support | ||
extension Result { | ||
@available(*, unavailable, renamed: "success") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need to check if
error
isAnyError
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 1a12328.