如何使用 cognito 在 android 中指定自定义错误消息

How to specify a custom error message in android using cognito

我目前有一个 android 应用程序使用 amazon-cognito-sdk 进行用户登录。当用户不存在或输入错误密码时显示的消息不是很好,所以我想自定义它,但我看不到任何方法?

我在工作中也做过类似的事情。基本上,我们将 SDK 异常映射到我们内部的 AuthenticationException class,每种类型都会有自己的消息。下面找到 kotlin 中的示例代码。

private fun toAuthError(exception: Exception): AuthenticationError {
    return when(exception) {
        is UserNotFoundException -> AuthenticationError.UserNotFound()
        is InvalidParameterException -> AuthenticationError.InvalidParameter()
        is NotAuthorizedException -> AuthenticationError.UserNotFound()
        is InvalidPasswordException -> AuthenticationError.InvalidPassword()
        is InvalidLambdaResponseException -> AuthenticationError.InvalidResponse()
        is LimitExceededException -> AuthenticationError.LimitExceeded()
        is UsernameExistsException -> AuthenticationError.UsernameExists()
        is UserNotConfirmedException -> AuthenticationError.UserNotConfirmed()
        is CodeMismatchException -> AuthenticationError.VerificationCodeMismatch()
        is ExpiredCodeException -> AuthenticationError.VerificationCodeExpired()
        else -> AuthenticationError.UnknownError()
    }
}

请注意,上述方法是在 onFailure 认知 AuthenticationHandler 回调中调用的。

        val authenticationHandler = object : AuthenticationHandler {
            ...
            override fun onFailure(exception: Exception) {
                Timber.e("login Failure $exception")
                subscriber.onError(toAuthError(exception))
            }
        }

这是 android kotlin 中的一个简单示例,说明 class 可能是什么样子,因为我发现这个 post 很有帮助。

class CognitoExceptionHelper(val context: Context) {

    /////////////////////////////////////////////////////////////////
    // STATIC MEMBERS
    /////////////////////////////////////////////////////////////////
    companion object {
        private var instance: CognitoExceptionHelper? = null

        @Synchronized
        fun getInstance(context: Context): CognitoExceptionHelper {
            if(instance == null){
                instance = CognitoExceptionHelper(context)
            }

            return instance!!
        }
    }

    /////////////////////////////////////////////////////////////////
    // METHODS
    /////////////////////////////////////////////////////////////////
    fun toAuthError(ex: Exception?): String {
        return when (ex) {
            is UserNotFoundException -> context.getString(R.string.user_not_found)
            is InvalidParameterException -> context.getString(R.string.invalid_parameter)
            is NotAuthorizedException -> context.getString(R.string.user_not_found)
            is InvalidPasswordException -> context.getString(R.string.invalid_password)
            is InvalidLambdaResponseException -> context.getString(R.string.invalid_lambda_response)
            is LimitExceededException -> context.getString(R.string.limit_exceeded)
            is UsernameExistsException -> context.getString(R.string.email_exists)
            is UserNotConfirmedException -> context.getString(R.string.account_not_confirmed)
            is CodeMismatchException -> context.getString(R.string.incorrect_verification_code)
            is ExpiredCodeException -> context.getString(R.string.verification_code_expired)
            is PasswordResetRequiredException -> context.getString(R.string.password_reset_required)
            else -> context.getString(R.string.error_occurred)
        }
    }
}