2016-05-24 36 views
3

當我嘗試打印Firebase用戶登錄的可讀,userfrendly錯誤消息時,發現我無法使用NSError.localizedDiscreption casue它會顯示「詳細查找更多內容」。如何從Firebase身份驗證的NSError登錄中獲取錯誤消息

我的錯誤信息是這樣的,當我使用print(error)命令

2016-05-23 21:29:33.035 Mission Board[3251:] <FIRAnalytics/INFO> Firebase Analytics enabled 

Optional(Error Domain=FIRAuthErrorDomain Code=17999 "An internal error has occurred, print and inspect the error details for more information." UserInfo={NSUnderlyingError=0x1277e2bb0 {Error Domain=FIRAuthInternalErrorDomain Code=3 "(null)" UserInfo={FIRAuthErrorUserInfoDeserializedResponseKey=<CFBasicHash 0x1288eec60 [0x1a172a150]>{type = immutable dict, count = 3, 

entries => 

    0 : <CFString 0x12890ad60 [0x1a172a150]>{contents = "message"} = <CFString 0x1288eec20 [0x1a172a150]>{contents = "INVALID_EMAIL"} 

    1 : errors = <CFArray 0x12890ad20 [0x1a172a150]>{type = immutable, count = 1, values = (

    0 : <CFBasicHash 0x12890c660 [0x1a172a150]>{type = immutable dict, count = 3, 

entries => 

    0 : reason = invalid 

    1 : message = <CFString 0x128872790 [0x1a172a150]>{contents = "INVALID_EMAIL"} 

    2 : domain = global 

} 



)} 

    2 : code = <CFNumber 0xb000000000001903 [0x1a172a150]>{value = +400, type = kCFNumberSInt64Type} 

} 

}}, error_name=ERROR_INTERNAL_ERROR, NSLocalizedDescription=An internal error has occurred, print and inspect the error details for more information.}) 

這是不是很可讀,但我在條目1碼0中發現,有一個與內容「無效的電子郵件」

消息

我可以只輸出該部分警報給用戶嗎?

有沒有內置的函數可以做到這一點?

+0

我認爲你必須威脅這個錯誤。用戶無法理解這一點。如果您嘗試不同的錯誤,例如密碼較弱,電子郵件已存在,您將看到錯誤以相同格式顯示。消息字段有錯誤的正確原因,但在代碼中。您可以將每個原因翻譯成一些錯誤信息,或者大部分信息。 – ByteArtisan

+0

我該如何修改它?我的意思是它不是一個字符串對嗎? –

+0

我的意思是爲每個可能的消息內容添加一些開關。 在這種情況下,「INVALID_EMAIL」應翻譯爲「輸入的電子郵件無效」。 – ByteArtisan

回答

0

下面的代碼是我註冊錯誤控制。我使用KVNProgress pod進行警報。

Swift3

import KVNProgress 

FIRAuth.auth()?.createUser(withEmail: self.emailTextLbl.text!, password: self.passwordTextLbl.text!, completion: { (user, error) in 

if error != nil { 

    let error2:NSError = error as! NSError 

    if error2.code == FIRAuthErrorCode.errorCodeInvalidEmail.rawValue { 

     KVNProgress.showError(withStatus: "Invalid Email", completion: nil) 

    }else if error2.code == FIRAuthErrorCode.errorCodeNetworkError.rawValue { 

    KVNProgress.showError(withStatus: "Network Error", completion: nil) 

    }else if error2.code == FIRAuthErrorCode.errorCodeWeakPassword.rawValue { 

    KVNProgress.showError(withStatus: "Weak Password", completion: nil) 

    }else if error2.code == FIRAuthErrorCode.errorCodeEmailAlreadyInUse.rawValue { 

    KVNProgress.showError(withStatus: "Email Already In Use", completion: nil) 

    } 


}else{ 


    // Your code 


} 

}) 

您可以找到 「FIRAuthErrorCode」 其他錯誤控制,如登錄錯誤處理。