细化twilio令牌连接错误

时间:2017-07-23 18:17:27

标签: swift twilio twilio-api

目前,用户将获得“没有互联网连接”#34;错误,当cloudfunctions API获取Twilio令牌离线或有故障时。这应该返回不同的错误消息。如何根据swift中遇到的错误类型细化错误消息?

class Token {
    let value: String
init (url: String = "userTokenURL") throws {
    guard let requestURL = URL(string: url) else {
        throw Token.Error.invalidURL
    }
    do {
        let data = try Data(contentsOf: requestURL)
        guard let stringToken = String(data: data, encoding: .utf8) else {
            throw Token.Error.couldNotConvertDataToString
        }
        value = stringToken
    } catch let error as NSError {
        print ("Error fetching token data, \(error)")
        throw Token.Error.noInternet
    }
}
}

extension Token {
enum Error: Swift.Error {
    case invalidURL
    case couldNotConvertDataToString
    case noInternet

    var description: String? {
        switch self {
        case .invalidURL:
            return NSLocalizedString("Token URL is invalid.", comment: "")
        case .couldNotConvertDataToString:
            return NSLocalizedString("Token Data could not be converted to String.", comment: "")
        case .noInternet:
            return NSLocalizedString("Internet connection failed.", comment: "")
        }
    }
}
}

0 个答案:

没有答案