salesforce - 在 Salesforce 中的 Apex 中捕获 LWC 中的异常
问题描述
我有下面的代码从 Apex 抛出异常
@AuraEnabled()
public static void associateAccount(string userId, string accountSAPNumber) {
if(String.isBlank(userId) || string.isBlank(accountSAPNumber)) {
throw new AuraHandledException('Please specify both User Email2 and SAP Number2');
}
List<user> users = [SELECT Id, Name,Email FROM User WHERE Email =: userId AND IsActive = true AND Profile.Name ='OEA Customer'];
if(users == null || users.size() <= 0) {
NoDataFoundException noUsersFound = new NoDataFoundException();
noUsersFound.setMessage('No users found with the specified email address: ' + userId);
throw noUsersFound;
}
Id accountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('OEA Customer Location').getRecordTypeId();
accountSAPNumber = '%' + accountSAPNumber;
List<Account> accounts = [SELECT Id FROM Account WHERE RecordTypeId =:accountRecordTypeId AND SAP_Number__c like :accountSAPNumber];
if(accounts == null || accounts.size() <= 0) {
NoDataFoundException noAccountFound = new NoDataFoundException();
noAccountFound.setMessage('No accounts found with the specified SAP Number: ' + accountSAPNumber);
throw noAccountFound;
}
else if(accounts.size() > 1) {
SearchException moreThan1Account = new SearchException();
moreThan1Account.setMessage('More than 1 account found with the specified SAP Number: ' + accountSAPNumber);
throw moreThan1Account;
}
OEA_NewContactFormController.accountMethod(userId, accountSAPNumber);
}
我无法使用以下方法在我的 LWC 控制器中捕获此异常
continueButtonClicked() {
associateAccount({
userId: this.searchKey,
accountSAPNumber: this.accountNumber,
})
.then((result) => {
try {
console.log("return from remote call " + result);
this.modalPopUpToggleFlag = false;
} catch (error) {
console.log('some error');
}
})
.error((error) => {
console.log("some error in code");
/*if (Array.isArray(error.body)) {
console.log(
"error message :" + error.body.map((e) => e.message).join(", ")
);
} else if (typeof error.body.message === "string") {*/
//console.log("error message :" + error);
//}
})
.finally(() => {
console.log("finally message :");
});
}
这总是在控制台上给我一个错误,如“未捕获(承诺中)”以及异常的详细信息。如何以处理的方式捕获此异常。
解决方案
正确的语法是.then().catch().finally()
,当你写的时候.then().error().finally()
。
而且 associateAccount 返回void
,所以不会有result
收到 from then
。也没有理由包含this.modalPopUpToggleFlag = false;
在 try-catch 中,只有当您从未定义过modalPopUpToggleFlag
可能有错误时。
continueButtonClicked() {
associateAccount({
userId: this.searchKey,
accountSAPNumber: this.accountNumber,
})
.then(() => {
console.log("return from remote call);
this.modalPopUpToggleFlag = false;
})
.catch((error) => {
console.log("some error in code:", error);
});
}
这是一篇关于在 Javascript 中使用 Promises的好读物
推荐阅读
- apache-spark - Pyspark agg function to "explode" rows into columns
- r - 从 R 运行 Vb 脚本
- python - Celery Bulk Execution 在 100 个任务成功运行后停止
- javascript - Javascript让函数在继续之前等待另一个函数
- typescript - TS 条件类型仅从联合中扩展一项
- swift - 切换到另一个calendarView时如何创建新的collectionView
- python - 无法在 swift 中接收从 ios 客户端发送的 python 套接字服务器中的图像
- python - Python 谷歌语音到文本转换
- docker - docker-compose - 我可以在不重新启动容器的情况下更新标签吗?
- angular - 默认选中复选框不适用于角度