处理 AWS Amplify onError 回调异常
Handling AWS Amplify onError Callback Exceptions
我无法理解如何处理 Android Java 中 Amplify 函数的 onError 回调。例如,下面是一个用于注册用户的 Amplify 调用。该函数工作得很好并且做了它应该做的事情,但是,我无法弄清楚如何处理可以用这个函数抛出的不同类型的 onError 回调,特别是因为它是用 Lambda 函数格式化的。
例如,如果用户已经注册,“错误”回调将变为“UsernameExistsException”,但我不知道如何过滤并处理该异常。
最终,我希望能够处理这个特定的异常以生成一个 AlertDialog,在这个例子中告诉用户“这个帐户已经存在”。
还有很多其他类似的函数,它们的结构与我想处理其他类型的错误的方式相同。
非常感谢任何帮助。有关如何使用这些函数的参考,请查看 Amplify 文档和教程 here。
已解决更新 1/23/21: 我使用@Jameson 的回答在下面发布了一个答案
Amplify.Auth.signUp(
user_email,
user_password,
AuthSignUpOptions.builder().userAttributes(attributes).build(),
result -> {
Log.i("AuthQuickStart", "Result: " + result.toString());
Intent intent = new Intent(SignUpActivity.this, SignUpAuthActivity.class);
Bundle bun = new Bundle();
bun.putSerializable("user", user);
intent.putExtras(bun);
startActivity(intent);
finish();
},
error -> {
//in the example, the Error becomes the UsernameExistsException callback
Log.e("AuthQuickStart", "Sign up failed", error);
}
);
正如您所注意到的,Amplify Android 的大多数方法都是 异步的 ,并在两个 回调之一中发出结果或错误。回调接口是“简单的功能接口”,因此可以使用 lambda 表达式语法进行简化。
在 Java 中,您可以像这样查找 UsernameExistsException
错误:
AuthSignUpOptions options =
AuthSignUpOptions.builder()
.userAttributes(attributes)
.build();
Amplify.Auth.signUp(username, password, options,
result -> { /* handle result ... */ },
error -> {
if (error instanceof UsernameExistsException) {
showAlreadyExistsDialog();
}
}
);
与:
private void showAlreadyExistsDialog() {
new AlertDialog.Builder(context)
.setTitle("User already exists!")
.setMessage("Tried to sign-up an already-existing user.")
.setPositiveButton(android.R.string.yes, (dialog, which) -> {
// on click...
})
.setNegativeButton(android.R.string.no, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
与 Kotlin 类似,除了您可以使用 when
构造:
val options = AuthSignUpOptions.builder()
.userAttributes(attributes)
.build()
Amplify.Auth.signUp(username, password, options,
{ /* result handling ... */ },
{ error ->
when (error) {
is UsernameExistsException ->
showAlreadyExistsDialog()
}
}
)
感谢@Jameson 提供一些见解。这是我最终用来处理来自 Amplify 的错误的解决方案。
有两点需要注意:
由于 Amplify 库使用一些旧的 Java 库和一些用于错误处理的新代码,我无法像@Jameson 的回答那样适当地转换错误。在 AuthExceptions 中设置 Amplify 库的方式不允许我转换抛出的根错误。我能够正确识别错误的唯一方法是使用 string.contains 函数扫描 Amplify 函数在错误期间输出的字符串。
此外,由于 lambda 函数格式,您必须 运行 lambda 函数内的线程才能与 UI 元素交互。否则会抛出循环错误。
Amplify.Auth.signIn(
user_email,
user_pass,
result -> {
Log.i("AmplifyAuth", result.isSignInComplete() ? "Sign in succeeded" : "Sign in not complete");
startActivity(new Intent(LoginActivity.this, MainDashActivity.class));
finish();
},
error -> {
Log.e("AmplifyAuth", error.toString());
//Handles incorrect username/password combo
if (error.getCause().toString().contains("Incorrect username or password") ) {
runOnUiThread(new Runnable() {
@Override
public void run() {
incorrectLoginDialog.show();
}
});
return;
}
//Handles too many login attempts
if (error.getCause().toString().contains("Password attempts exceeded") ) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tooManyAttemptsDialog.show();
}
});
return;
}
//Handles when the email user does not exist in the user pool
if (error.getCause().toString().contains("User does not exist") ) {
runOnUiThread(new Runnable() {
@Override
public void run() {
doesNotExistDialog.show();
}
});
return;
}
}
);
我无法理解如何处理 Android Java 中 Amplify 函数的 onError 回调。例如,下面是一个用于注册用户的 Amplify 调用。该函数工作得很好并且做了它应该做的事情,但是,我无法弄清楚如何处理可以用这个函数抛出的不同类型的 onError 回调,特别是因为它是用 Lambda 函数格式化的。
例如,如果用户已经注册,“错误”回调将变为“UsernameExistsException”,但我不知道如何过滤并处理该异常。 最终,我希望能够处理这个特定的异常以生成一个 AlertDialog,在这个例子中告诉用户“这个帐户已经存在”。
还有很多其他类似的函数,它们的结构与我想处理其他类型的错误的方式相同。
非常感谢任何帮助。有关如何使用这些函数的参考,请查看 Amplify 文档和教程 here。
已解决更新 1/23/21: 我使用@Jameson 的回答在下面发布了一个答案
Amplify.Auth.signUp(
user_email,
user_password,
AuthSignUpOptions.builder().userAttributes(attributes).build(),
result -> {
Log.i("AuthQuickStart", "Result: " + result.toString());
Intent intent = new Intent(SignUpActivity.this, SignUpAuthActivity.class);
Bundle bun = new Bundle();
bun.putSerializable("user", user);
intent.putExtras(bun);
startActivity(intent);
finish();
},
error -> {
//in the example, the Error becomes the UsernameExistsException callback
Log.e("AuthQuickStart", "Sign up failed", error);
}
);
正如您所注意到的,Amplify Android 的大多数方法都是 异步的 ,并在两个 回调之一中发出结果或错误。回调接口是“简单的功能接口”,因此可以使用 lambda 表达式语法进行简化。
在 Java 中,您可以像这样查找 UsernameExistsException
错误:
AuthSignUpOptions options =
AuthSignUpOptions.builder()
.userAttributes(attributes)
.build();
Amplify.Auth.signUp(username, password, options,
result -> { /* handle result ... */ },
error -> {
if (error instanceof UsernameExistsException) {
showAlreadyExistsDialog();
}
}
);
与:
private void showAlreadyExistsDialog() {
new AlertDialog.Builder(context)
.setTitle("User already exists!")
.setMessage("Tried to sign-up an already-existing user.")
.setPositiveButton(android.R.string.yes, (dialog, which) -> {
// on click...
})
.setNegativeButton(android.R.string.no, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
与 Kotlin 类似,除了您可以使用 when
构造:
val options = AuthSignUpOptions.builder()
.userAttributes(attributes)
.build()
Amplify.Auth.signUp(username, password, options,
{ /* result handling ... */ },
{ error ->
when (error) {
is UsernameExistsException ->
showAlreadyExistsDialog()
}
}
)
感谢@Jameson 提供一些见解。这是我最终用来处理来自 Amplify 的错误的解决方案。
有两点需要注意: 由于 Amplify 库使用一些旧的 Java 库和一些用于错误处理的新代码,我无法像@Jameson 的回答那样适当地转换错误。在 AuthExceptions 中设置 Amplify 库的方式不允许我转换抛出的根错误。我能够正确识别错误的唯一方法是使用 string.contains 函数扫描 Amplify 函数在错误期间输出的字符串。
此外,由于 lambda 函数格式,您必须 运行 lambda 函数内的线程才能与 UI 元素交互。否则会抛出循环错误。
Amplify.Auth.signIn(
user_email,
user_pass,
result -> {
Log.i("AmplifyAuth", result.isSignInComplete() ? "Sign in succeeded" : "Sign in not complete");
startActivity(new Intent(LoginActivity.this, MainDashActivity.class));
finish();
},
error -> {
Log.e("AmplifyAuth", error.toString());
//Handles incorrect username/password combo
if (error.getCause().toString().contains("Incorrect username or password") ) {
runOnUiThread(new Runnable() {
@Override
public void run() {
incorrectLoginDialog.show();
}
});
return;
}
//Handles too many login attempts
if (error.getCause().toString().contains("Password attempts exceeded") ) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tooManyAttemptsDialog.show();
}
});
return;
}
//Handles when the email user does not exist in the user pool
if (error.getCause().toString().contains("User does not exist") ) {
runOnUiThread(new Runnable() {
@Override
public void run() {
doesNotExistDialog.show();
}
});
return;
}
}
);