我正在尝试处理有角度的http请求中的错误,但总是收到“内部服务器错误”而不是消息
但是代码中没有正确捕获
这是我的服务:
import { Injectable } from '@angular/core';
import { ConfigService } from '../config/config.service';
import { HttpClient, HttpErrorResponse } from "@angular/common/http";
import { Observable, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
@Injectable({
providedIn: "root"
})
export class ProxyService {
private proxyUrl = ConfigService.settings.proxyServer.url + "/v1";
constructor(private http: HttpClient) {}
postProxy(url: string,form: FormData,responseType: any): Observable<any> {
const requestUrl = `${this.proxyUrl}/${url}`;
return this.http.post(requestUrl, form, {
responseType: responseType
})
.pipe(
map( (res) => res),
catchError((e:any) => this.errorHandler(e))
)
}
errorHandler(error: HttpErrorResponse){
console.log('errorHandle',error);
console.log('errorHandle message?', error.message);
return throwError(error.error)
}
}
这就是我在组件请求中所做的:
this.proxyService.postProxy(proxyRequestUrl, proxyRequestForm, responseType).subscribe(
data => {
this.result.type = "image";
this.createImageFromBlob(data);
},
(error) => {
console.error("error", error);
console.log(this.errors);
// console.error(error);
this.errors.push(error);
},
() => {
console.log('finished')
this.demoLoading = false;
}
)
在执行api调用的组件中,我进入了错误部分,但没有正确的消息:
该请求也从未在我的console.log('finished')
上完成我正在使用rxjs 6.3.3和Angular 7.2
答案 0 :(得分:0)
根据documentation中的示例代码,我认为您正在查看错误的属性。
我认为响应的正文位于属性error.error
中,因此您的处理程序将如下所示:
errorHandler(error: HttpErrorResponse){
console.log('errorHandle',error);
console.log('errorHandle message?', error.error.message);
return throwError(error.error)
}