所以我正在使用httpClient.get()进行REST调用。我已经将API设置为返回超时504。
在查看Google devTools时,我可以看到已经进行了调用并且状态为504。但是当我调试代码时,我得到的HttpErrorResponse的状态为0。
我还设置了API以返回400响应,并且在调试该代码时,响应状态为400。为什么传播了400,但没有传播504?
我们正在使用Angular 6.0.6
答案 0 :(得分:0)
更好的方法是实现错误处理程序。 就我而言,我有一个公共类来进行HTTP调用,并且所有服务(例如:EmployeeService,SupplierService等)都将与公共API通信以进行Rest API调用。下面是处理错误的示例代码。这是HTTP状态代码为404的情况。
服务器端错误-代码:404 消息:针对http://localhost:5000/apiv1/account的Http错误响应:404未找到
这是我的共同服务模样。
@Injectable({
providedIn: 'root'
})
export class ApiService {
getUrlvalue: string;
constructor(private http: HttpClient) { }
private formatErrors(error: any) {
let errorMessage = '';
if (error.error instanceof ErrorEvent) {
errorMessage=`Client side Error: ${error.error.message}`;
} else {
errorMessage=`Server side Error - Code: ${error.status}\nMessage: ${error.message}`;
}
console.log(errorMessage);
return throwError(error.error);
}
get(path: string, params: HttpParams = new HttpParams()): Observable<any> {
//console.log(`${environment.api_url}${path}`);
this.getUrlvalue = `${environment.api_url}${path}`;
console.log('API service get request is making to : ' + this.getUrlvalue);
return this.http.get(this.getUrlvalue, { params }) //, { params }
.pipe(
catchError(this.formatErrors)
);
}
put(path: string, body: Object = {}): Observable<any> {
console.log(`${environment.api_url}${path}`);
return this.http.put(
`${environment.api_url}${path}`,
JSON.stringify(body), httpHeaderOptions
).pipe(catchError(this.formatErrors));
}
post(path: string, body: Object = {}): Observable<any> {
console.log(`${environment.api_url}${path}`);
return this.http.post(
`${environment.api_url}${path}`,
JSON.stringify(body), httpHeaderOptions
).pipe(catchError(this.formatErrors));
}
delete(path): Observable<any> {
console.log(`${environment.api_url}${path}`);
return this.http.delete(
`${environment.api_url}${path}`
).pipe(catchError(this.formatErrors));
}
}
从其他服务链接AccountService调用简单HTTP Get的方式
@Injectable({
providedIn: 'root'
})
export class AccountService {
// private http: HttpClient
constructor(private apiService: ApiService) { }
getAccounts(): Observable<Deal[]> {
return this.apiService.get('/account')
.pipe(
tap(deals => console.log('fetched Accounts 111111')),
catchError(this.handleError('getAccounts', []))
);
}
答案 1 :(得分:0)
结果是我们的API网关设置不正确,并且没有返回正确的标头。
iris %>%
mutate(long_sepal = Sepal.Length > 5.5) %>%
count(Species, long_sepal) %>%
mutate(y_label = if_else(long_sepal, 0.25, 1)) %>% # Now the text moves, but not where I expect
ggplot(aes(x = Species)) +
geom_col(aes(y = n, fill = long_sepal), position = position_fill()) +
geom_text(
mapping = aes(label = n, y = y_label, group = long_sepal),
hjust = 0,
position = position_fill()
) +
coord_flip()