Angular 承诺捕获不拦截异常
我已经为我的承诺编写了一个catch,但它似乎没有拦截.NETCoreAPI引发的异常。正如您在下面的代码中所看到的,我正在从我的api发送一个异常。 即使从api抛出异常,也不会执行catch块。 我不确定问题出在哪里 角分量Angular 承诺捕获不拦截异常,angular,asp.net-core,Angular,Asp.net Core,我已经为我的承诺编写了一个catch,但它似乎没有拦截.NETCoreAPI引发的异常。正如您在下面的代码中所看到的,我正在从我的api发送一个异常。 即使从api抛出异常,也不会执行catch块。 我不确定问题出在哪里 角分量 private getRepCode = (repCode: string) => { this.loading = true; const repToSend = fromGenistarCode(repCode); if (repToS
private getRepCode = (repCode: string) => {
this.loading = true;
const repToSend = fromGenistarCode(repCode);
if (repToSend === 0) {
this.splitSaleDetails.repName = '';
return;
}
this.inviteService.getRepApprovedName(repToSend, this.currentUserId).then(rep => {
if (rep) {
this.splitSaleDetails.repName = rep.name;
} else {
this.splitSaleDetails.repName = '';
}
this.fieldDataChanged(new SetSplitSaleDetails(this.splitSaleDetails));
this.loading = false;
}).catch((err) => {
console.log(err.message);
return this.messageService.add(err.message, 'warning', true);
});
角度服务
public getRepApprovedName = (repId: number, loggedInUserId: number): Promise<any> =>
this.http.get(`${this.baseUrl}representative-name/${repId}/${loggedInUserId}`).toPromise()
public getRepApprovedName=(repId:number,loggedInUserId:number):Promise=>
this.http.get(`${this.baseUrl}代表名称/${repId}/${loggedInUserId}').toPromise()
.NETAPI
[FunctionName(nameof(GetRepresentativeName))]
[UsedImplicitly]
public Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "organisation/representative-name/{id}/{loggedInId}")]
HttpRequest req,
int id, int loggedInId) => _helper.HandleAsync(async () =>
{
await _helper.ValidateRequestAsync(req);
var person = await _context.Person
.Include(x => x.Representative)
.FirstOrDefaultAsync(x => x.Id == id
&& x.Id != loggedInId
&& x.Representative.ApprovalDateTime != null
&& x.Representative.PaymentDateTime != null
&& x.Representative.TerminatedDateTime == null);
return person != null
? (IActionResult)new OkObjectResult(new
{
name = $"{person.FirstName} {person.LastName}"
})
: new NotFoundObjectResult(new JsonError { Message = @"<html><body>
<ul>
<li>You cannot split a sale with Yourself</li>
<li>A representative that has not completed their IBA and not been approved by compliance.</li>
<li>A terminated representative.</li>
</ul>
</body></html>", Name = nameof(InvalidOperationException) });
});
}
[FunctionName(nameof(GetRepresentativeName))]
[正确使用]
公共任务运行(
[HttpTrigger(AuthorizationLevel.Anonymous,“get”,Route=“organization/representative name/{id}/{loggedInId}”)]
HttpRequest请求,
int id,int loggedInId)=>\u helper.HandleAsync(异步()=>
{
等待帮助程序验证请求同步(req);
var person=wait_context.person
.包括(x=>x.代表)
.FirstOrDefaultAsync(x=>x.Id==Id
&&x.Id!=loggedInId
&&x.Representative.ApprovalDateTime!=null
&&x.Representative.PaymentDateTime!=null
&&x.Representative.TerminatedDateTime==null);
返回人!=null
?(IActionResult)新OkObjectResult(新
{
name=$“{person.FirstName}{person.LastName}”
})
:new NotFoundObjectResult(new JsonError{Message=@)
- 您不能自己分割销售
- 尚未完成IBA且未经合规部批准的代表
- 被解雇的代表
,Name=nameof(InvalidOperationException)};
});
}
可能是因为您没有在getRepApprovedName()
中返回API,但是您可以看到getRepApprovedName正在调用端点。我已经调试了代码,可以看到调用的api在Chrome devtools网络选项卡中的响应状态是什么?响应是{“name”:“invalidooperationexception”,“message”:“\r\n- \r\n
- 您不能与自己分割销售 \r\n
- 未完成IBA且未经合规部批准的代表。 \r\n
- 终止的代表。 \r\n
getRepApprovedName()中返回API”
但正如您所看到的,getRepApprovedName正在调用端点。我已经调试了代码,可以看到正在调用的api。Chrome devtools网络选项卡中的响应状态是什么?响应是{“name”:“InvalidOperationException”,“message”:\r\n- \r\n
- 您不能与自己分割销售 \r\n
- 未完成IBA且未经合规部批准的代表。 \r\n
- 终止的代表。 \r\n