Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zyg-github
Forked from Kametrixom/ContextError.swift
Created December 28, 2015 06:28
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 zyg-github/ba7fca948183ed9d0d6b to your computer and use it in GitHub Desktop.
Save zyg-github/ba7fca948183ed9d0d6b to your computer and use it in GitHub Desktop.
protocol ContextError : ErrorType {
mutating func addContext<T>(type: T.Type)
}
protocol Contextualizable {}
extension Contextualizable {
func addContext(var error: ContextError) -> ContextError {
error.addContext(self.dynamicType)
return error
}
}
struct Error: ContextError {
var source : String
let reason : String
init(reason: String, file: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
self.reason = reason
self.source = "\(file):\(function):\(line)"
}
mutating func addContext<T>(type: T.Type) {
source += ":\(type)"
}
}
struct Parent: Contextualizable {
func myFunction() throws {
throw addContext(Error(reason: "An important reason"))
}
}
do {
try Parent().myFunction()
} catch {
print(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment