我的订阅没有处理错误

My subscription is not handling errors

我订阅了一项服务,我从外部 API 请求图像 URL 并将其存储在共享服务中。

逻辑:
1.清空当前图片数组
2.从API请求图片,推入(新清空)数组(response =>)
3. 如果 URL returns 出错,用默认(占位符)图像填充数组 (error =>)

由于某种原因,尽管存在一个错误,但仍未得到处理。 我已经发布了我的组件和上下文错误消息。

Component.ts

  getImages(vehicleID: number, color: string): void {
    this._carConfigService.car.colors.exterior.urls = [];
    this._FAPIService.getImagesForColors(vehicleID, color)
      .subscribe(
        response => {
        response.products[0].productFormats.forEach(pF => {
        this._carConfigService.car.colors.exterior.urls.push(pF.assets[0].url);
        });
      },
        error => {
          this.errorMessage = <any>error;
          if (this._carConfigService.car.colors.exterior.urls.length === 0) {
            this._carConfigService.car.colors.exterior.urls = this._carConfigService.defaultImages;
          }
      });
  }

我收到以下错误:

Failed to load resource: the server responded with a status of 422 ()
EXCEPTION: Response with status: 0  for URL: null
Uncaught Response

您需要使用常规的异常处理方法来处理它们

export class TaskService {
    private _url = "api/products/datanew.json";
    constructor(private _http: Http) {
    }
    getTasks(): Observable<any[]> {
        return this._http.get(this._url)
            .map((response: Response) => <any[]>response.json())
            .do(data => console.log("data we got is " + JSON.stringify(data)))
            .catch(this.handleError);

    }
    private handleError(error: Response) {
            console.log(error);
            return Observable.throw(error.json().error || 'Internal Server error');
    }
}

另外,我有一个问题。

  1. 您正在获取数据但没有分配给任何对象?没有return类型