Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
public enum Error: ErrorType, CustomDebugStringConvertible {
// Add error enumerations here
case FirstError(source: String, reason: String)
public var debugDescription: String {
let mirror = Mirror(reflecting: self)
guard let item = mirror.children.first else {fatalError("Missing error enumeration item")}
guard let myEnumeration = item.label else {fatalError("Missing error enumeration label")}
let itemMirror = Mirror(reflecting:item.value)
guard let sourceItem = itemMirror.children.first else {fatalError("Missing error source child")}
guard let reasonItem = itemMirror.children.dropFirst().first else {fatalError("Missing error reason child")}
let (_, sourceStringItem) = sourceItem
let (_, reasonStringItem) = reasonItem
guard let reasonString = reasonStringItem as? String else {fatalError("Missing error reason string value")}
return "\(mirror.subjectType).\(myEnumeration): \(sourceStringItem)" + (reasonString.isEmpty ? "" : ", \"\(reasonString)\"")
// Call with
// Or"reason text", type:Error.FirstError)
static func build(
file : String = __FILE__,
function : String = __FUNCTION__,
line : Int = __LINE__,
reason : String = "",
type errorType: (String, String) -> Error) -> Error {
return errorType("File \(file), Line \(line)", reason)
let f =
let e ="Something", type:Error.FirstError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment