Last active
May 12, 2018 17:52
-
-
Save erica/90ee67c051695267b2ff5de5cb44cf32 to your computer and use it in GitHub Desktop.
This file contains 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
import Foundation | |
// Inspired by Maz Jaleel, https://github.com/Mazyod | |
/// Unconditionally prints a given message and stops execution, | |
/// allowing placement at any point where a contextual type | |
/// is required. | |
/// | |
/// For example, use to fail on unwrap: | |
/// | |
/// ``` | |
/// // This URL is well formed | |
/// let normalURL = URL(string: "http://swift.org") | |
/// ?? fail("URL is not well formed") | |
/// | |
/// // This URL is not well formed and should raise a fatal error | |
/// let emojiURL = URL(string: "😱") | |
/// ?? fail("URL is not well formed") | |
/// ``` | |
/// | |
/// - Parameters: | |
/// - message: The string to print. The default is an empty string. | |
/// - file: The file name to print with `message`. The default is the file | |
/// where `fail(_:file:line:)` is called. | |
/// - line: The line number to print along with `message`. The default | |
/// is the line number where `fail(_:file:line:)` is called. | |
public func fail<T>( | |
_ message: String = "", | |
file: StaticString = #file, | |
line: UInt = #line | |
) -> T { | |
fatalError(message, file: file, line: line) | |
} | |
/// Unconditionally throw an error, allowing placement at any point where | |
/// a contextual type is required. | |
/// | |
/// Note the use of `try` in the nil-coalescing calls in the following | |
/// example: | |
/// | |
/// ``` | |
/// public struct ErrorMessage: Error { | |
/// public let info: String | |
/// public init(_ info: String) { | |
/// self.info = info | |
/// } | |
/// } | |
/// | |
/// let illformedURL = ErrorMessage("URL is not well formed") | |
/// | |
/// do { | |
/// /// This URL is well formed | |
/// let normalURL = try URL(string: "http:/// swift.org") | |
/// ?? fail(illformedURL) | |
/// | |
/// /// This URL is not well formed and will throw | |
/// let emojiURL = try URL(string: "😱") ?? fail(illformedURL) | |
/// } catch { | |
/// print(error) | |
/// } | |
/// ``` | |
/// - Parameters: | |
/// - error: The error to throw. | |
public func fail<T, E: Error>(_ error: E) throws -> T { | |
throw error | |
} | |
public struct ErrorMessage: Error { | |
public let info: String | |
public init(_ info: String) { | |
self.info = info | |
} | |
} | |
let illformedURL = ErrorMessage("URL is not well formed") | |
do { | |
/// This URL is well formed | |
let normalURL = try URL(string: "http:/// swift.org") ?? fail(illformedURL) | |
/// This URL is not well formed and will throw | |
let emojiURL = try URL(string: "😱") ?? fail(illformedURL) | |
} catch { | |
print(error) | |
} | |
// This URL is well formed | |
let normalURL = URL(string: "http://swift.org") | |
?? fail("URL is not well formed") | |
// This URL is not well formed and will fatal error | |
let emojiURL = URL(string: "😱") | |
?? fail("URL is not well formed") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment