Created
May 15, 2021 22:13
-
-
Save ollieatkinson/8dd4d6826b3842df5a14d2ea41fde412 to your computer and use it in GitHub Desktop.
Stringy Error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Optional { | |
public func or(throw error: @autoclosure () -> Error) throws -> Wrapped { | |
guard let wrapped = self else { throw error() } | |
return wrapped | |
} | |
} | |
extension String { | |
public func error( | |
_ function: String = #function, | |
_ file: String = #file, | |
_ line: Int = #line | |
) -> Error { | |
.init(message: self, function: function, file: file, line: line) | |
} | |
public struct Error: Swift.Error, CustomStringConvertible, CustomDebugStringConvertible { | |
let message: String | |
let function: String | |
let file: String | |
let line: Int | |
public var description: String { message } | |
public var debugDescription: String { "\(message) ← \(file)#\(line)" } | |
} | |
} | |
extension Error { | |
public var message: String { description } | |
} |
Author
ollieatkinson
commented
May 15, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment