【发布时间】:2021-06-24 13:44:27
【问题描述】:
我需要捕捉由我的 REST API 生成的响应主体,它使用自定义响应主体抛出 http 错误 406,但在我的角度,我只能捕捉到“不可接受”的标头状态,我无法获取身体,我怎样才能得到身体而不是“不可接受”的字符串?我也遇到了这个错误TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.,我不知道是哪个部分触发了这个错误。这是我的做法:
首先我创建全局服务类,它看起来像这样:
constructor(
private http: HttpClient,
private alertx: AlertService) { }
post<T>(url: string, body?: any, header?: HttpHeaders, param?: HttpParams): Observable<HttpResponse<T>>{
return this.http.post<T>(url, body,
{headers : header ? header : this.headers,
observe: 'response',
responseType: 'json',
params: param})
.pipe(catchError(err => {
const error = err.error?.error_description || err.error?.message || err.statusText;
console.log(err);
console.log('here i am');
return throwError(error);
}))
.pipe(timeout(10000), catchError(err => {
if (err instanceof TimeoutError) {
this.alertx.error('Timeout Exception');
return throwError('Timeout Exception');
}
}))
.pipe(shareReplay());
}
我试图控制台记录错误,它只是出现字符串'不可接受',然后它继续控制台.log('here i am');它打印正确,我找不到TypeError的原因在哪里
这是我如何使用上面的函数:
requestCancel(data: SelectItem, reasonCancel: string): Observable<any> {
const request: RequestResponse<SelectItem> = new RequestResponse<SelectItem>(data);
request.message = reasonCancel;
return this.rest.post<any>(`${environment.apiUrl}${endpoint.sales_order.request_cancel}`, request).pipe(map(
response => {
return response.body.data;
}));
}
当我尝试 console.log 响应时,它没有出现。我认为我的函数在我的全局服务类的 post 函数中停止工作,我做错了什么?
------------------ 更新我的代码 --------------------强>
我创建 HttpRequestInterceptor 类来处理错误,这是我的类:
@Injectable()
export class HttpRequestInterceptor implements HttpInterceptor {
user: UserModel;
prevUrl: string = '';
constructor(
private loginSvc: LoginService,
private alertx: AlertService,
private sharedService: SharedService) {
this.sharedService.currentUserSubject.subscribe(user => this.user = user);
}
intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.sharedService.setLoading(true, httpRequest.url);
return next.handle(httpRequest).pipe(catchError(err => {
if ([401, 403].includes(err.status) /* && this.user */) {
// auto logout if 401 or 403 response returned from api
this.sharedService.setLoading(false, httpRequest.url);
this.loginSvc.removeLocal();
}
this.sharedService.setLoading(false, httpRequest.url);
const error = err.statusText || err.statusText + ' ' + err.error?.message || err.statusText + ' ' + err.error?.error_description;
this.alertx.error(error, {autoClose: true});
return throwError(err.error?.message);
}))
.pipe(map<HttpEvent<any>, any>((evt: HttpEvent<any>) => {
if (evt instanceof HttpResponse) {
this.sharedService.setLoading(false, httpRequest.url);
}
return evt;
}));
}
}
当我 console.log 我的err.error?.message 我可以正确捕获错误,然后我将值传递给这里:
post<T>(url: string, body?: any, header?: HttpHeaders, param?: HttpParams): Observable<HttpResponse<T>>{
return this.http.post<T>(url, body,
{headers : header ? header : this.headers,
observe: 'response',
responseType: 'json',
params: param})
.pipe(catchError(err => {
console.log(err);
return throwError(err);
}))
.pipe(timeout(10000), catchError(err => {
if (err instanceof TimeoutError) {
this.alertx.error('Timeout Exception');
return throwError('Timeout Exception');
}
}))
.pipe(shareReplay());
}
直到那里我仍然可以正确捕获错误,当我 console.log(err) 但是当我使用 post 函数时,我无法捕获错误,并且我在浏览器控制台中出现错误。这是我的功能:
requestCancel(data: SelectItem, reasonCancel: string): Observable<any> {
const request: RequestResponse<SelectItem> = new RequestResponse<SelectItem>(data);
request.message = reasonCancel;
return this.rest.post<any>(`${environment.apiUrl}${endpoint.sales_order.request_cancel}`, request).pipe(map(
response => {
return response.body.data;
}))
.pipe(catchError(err => {
// i cannot get anything here
console.log(err);
return err;
}));
}
我无法记录我的错误,它不会打印任何内容,我认为我的函数崩溃并抛出此错误:
TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
我还错过了什么?
【问题讨论】:
标签: angular typescript observable