gpt4 book ai didi

javascript - Angular 7 错误通知不显示

转载 作者:行者123 更新时间:2023-12-02 23:52:10 25 4
gpt4 key购买 nike

我继承了一个非常大的 Angular 应用程序,我必须将其从 5.2 升级到 7.2。我设法做到了这一点,但有一个错误我无法解决。

//编辑

我找到了解决方案,但我不知道它是否有效..

processReportResponse(response: HttpResponse<any>) {
if (response.status === 204) {
throw new HttpErrorResponse({ error: 'NoDataError' });
} else {
return new Blob([response.body], { type: 'application/pdf' });
}
}

//结束编辑

我们正在根据某些输入生成报告。当您点击生成时,createdSalesByDateReport函数被调用,如下所示:

createSalesByDateReport() {
const fromTemp = new Date(this.from);
let toTemp = new Date(this.to);

if (this.selectMonth) {
// select the whole month of the from date
fromTemp.setDate(1);
toTemp = new Date(fromTemp.getFullYear(), fromTemp.getMonth() + 1, 0);
toTemp.setHours(23, 59, 59, 999);
// setting day to 0 means one day less than first day of the month
}

this.startLoading();
if (!this.userSelectionDisabled && this.selectedUserIds && this.selectedUserIds.length > 0) {
this.createUserSalesReport(fromTemp, toTemp, this.selectedUserIds);
} else {
this.createOrganizationSalesReport(fromTemp, toTemp, this.selectedUserGroupIds);
}
}

由于我们正在创建用户销售报告,因此始终是 createUserSalesReport()正在被调用:

createUserSalesReport(from: Date, to: Date, userIds: string[]) {
this.reportService.getOrganizationUsersSalesReport(from, to, userIds)
.subscribe(
reportResponse => this.userGroupsSalesResponse(reportResponse, from, to),
res => this.processServerException(res)
);
}

这将调用报告服务的 getOrganizationUsersSalesReport()功能:

getOrganizationUsersSalesReport(from: Date, to: Date, userIds: string[]): Observable<any> {
return this.http.get(this.url + 'userSales?from=' + from.getTime() +
'&to=' + to.getTime() + '&timezone=' + Intl.DateTimeFormat().resolvedOptions().timeZone + '&userIds='
+ userIds.map(function (userId) { return userId; }),
{ observe: 'response', responseType: 'blob' })
.pipe(map(res => {
this.processReportResponse(res);
}));
}

这将转到服务的 processReportResponse()功能:

processReportResponse(response: any) {
if (response.status === 204) {
throw throwError('NoDataError');
} else {
return new Blob([response.body], { type: 'application/pdf' });
}
}

由于我们订阅了组件的 createUserSalesReport() 中的响应函数(如上所示),它要么是错误,要么是 Blob。

在我的例子中,应该会弹出一个通知并显示给定的错误,因为该服务的 processReportResponse()得到了 HttpResponse状态为 204,这会将错误传递给组件的 createUserSalesReport()函数,所以 processServerException()函数被调用:

processServerException(res: any) {
console.log(res);
this.stopLoading();
if (res.error && res.error === 'NoDataError') {
this.alertService.info(this.translateService.instant('reporting.nodata'));
}
}

但不幸的是,响应的格式是我无法检查或读取的,因此条件不会将响应的内容传递给 alertService

这是响应的格式:

enter image description here

当我可以检查错误响应的内容以及 processServerException() 中的条件时,我希望获得这种格式的错误响应。可以传给alertService .

最佳答案

你试过这个吗:

import { Observable, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
processReportResponse(response: HttpResponse<any>) {
if (response.status === 204) {
throw throwError({error: 'NoDataError'});
} else {
return new Blob([response.body], { type: 'application/pdf' });
}}

关于javascript - Angular 7 错误通知不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55594558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com