Created
December 19, 2017 03:36
-
-
Save luisburgos/267d59e176e1c35dfbc8ebd5eafdb24f 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
// | |
// Networking.swift | |
// YellowPod | |
// | |
// Created by Luis Burgos on 11/25/17. | |
// Copyright © 2017 YellowPod. All rights reserved. | |
// | |
import Foundation | |
import SwiftyJSON | |
enum SerializationError: Error { | |
case missing(String) | |
case invalid(String, Any) | |
var message: String { | |
switch self { | |
case .missing(let error): | |
return "Missing parameter: \(error)" | |
case .invalid(let error, _): | |
return "Invalid data \(error)" | |
} | |
} | |
} | |
/*Enable to throw a Key as error*/ | |
extension Keys: Error {} | |
extension JSON { | |
func guarantee(hasKeys: Keys...) throws { | |
try hasKeys.forEach { key in | |
guard self[key.rawValue].exists() else { throw SerializationError.missing(key.rawValue) } | |
} | |
} | |
func parse(_ key: Keys) -> JSON { | |
return self[key.rawValue] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment