add error description to JSONErrors
This commit is contained in:
parent
f8dec38c13
commit
89d9809b9e
@ -18,6 +18,30 @@ public enum JSONError: Error {
|
|||||||
case error(message: String)
|
case error(message: String)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension JSONError: LocalizedError, CustomStringConvertible {
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
switch self {
|
||||||
|
case .pathNotFound:
|
||||||
|
return "JSON path not found"
|
||||||
|
case .data(path: let path):
|
||||||
|
return "JSON data in \(path) is corrupt"
|
||||||
|
case .other(error: let error):
|
||||||
|
if let decodingError = error as? DecodingError {
|
||||||
|
// the only way to get the decoding error description and details...
|
||||||
|
return (decodingError as NSError).description
|
||||||
|
}
|
||||||
|
return error.localizedDescription
|
||||||
|
case .error(message: let message):
|
||||||
|
return message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public var errorDescription: String? {
|
||||||
|
return description
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extension JSONDictionary {
|
extension JSONDictionary {
|
||||||
|
|
||||||
public func toJSONString(options: JSONSerialization.WritingOptions = []) throws -> String {
|
public func toJSONString(options: JSONSerialization.WritingOptions = []) throws -> String {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user