Last active
February 3, 2018 22:58
-
-
Save erica/b6f4884ed5d70c269107 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
public struct Error: ErrorType { | |
let source: String; let reason: String | |
public init(_ source: String = __FILE__, _ reason: String) { | |
self.reason = reason; self.source = source | |
} | |
} | |
protocol Contextualizable {} | |
extension Contextualizable { | |
func functionContext(function : String = __FUNCTION__) -> String { | |
return "\(self.dynamicType):\(function)" | |
} | |
func fileContext(file : String = __FILE__, line : Int = __LINE__) -> String { | |
return "\(file):\(line)" | |
} | |
func currentContext(file : String = __FILE__, function : String = __FUNCTION__, line : Int = __LINE__) -> String { | |
return "\(self.dynamicType):\(function):\(file):\(line)" | |
} | |
func contextString(items: Any..., file : String = __FILE__, function : String = __FUNCTION__, line : Int = __LINE__) -> String { | |
return "\(function):\(self.dynamicType):\(file):\(line) " + items.map({"\($0)"}).joinWithSeparator(", ") | |
} | |
func contextPrint(items: Any..., file : String = __FILE__, function : String = __FUNCTION__, line : Int = __LINE__) { | |
print("\(function):\(self.dynamicType):\(file):\(line) " + items.map({"\($0)"}).joinWithSeparator(", ")) | |
} | |
func contextError(items: Any..., file : String = __FILE__, function : String = __FUNCTION__, line : Int = __LINE__) -> Error { | |
return Error("\(function):\(self.dynamicType):\(file):\(line) ", items.map({"\($0)"}).joinWithSeparator(", ")) | |
} | |
} | |
public struct Parent: Contextualizable { | |
func myFunction() throws { | |
throw contextError("Some good reason", 2, 3) | |
} | |
} | |
func dotry(block: () throws -> Void) { | |
do {try block()} catch {print(error)} | |
} | |
dotry {try Parent().myFunction()} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment