Skip to content

Instantly share code, notes, and snippets.

@erica
Last active May 12, 2018 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/181e5e84e62e8e2ee1a2ce40f876c1b3 to your computer and use it in GitHub Desktop.
Save erica/181e5e84e62e8e2ee1a2ce40f876c1b3 to your computer and use it in GitHub Desktop.
import Foundation
import Swift
//345678901234567890123456789012345678901234567890123456789012345678901234567890
// 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