使用RXJS忽略flatMap的错误

时间:2016-06-07 00:20:32

标签: angular rxjs

我试图使用flatmap制作一堆并行请求。问题是,如果我得到404,整个链断开,其余的请求被取消。有没有办法捕获404并返回空响应? getIterationsDashboard是我运行的初始方法。

getAllProjects(): Observable<Models.Project[]> {
    var that = this;

    var authHeader = new Headers();
    authHeader.append('Authorization', `Basic ${new Buffer('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:').toString('base64')}`);
    authHeader.append('X-TFS-FedAuthRedirect', 'Suppress');

    return this.http.get(`${this.baseUrl}`, {
        headers: authHeader
    }).map(res => {
        let body: Models.GetProjectsResponse = res.json();

        return body.value;
    });
}

getAllTeams(project: Models.Project): Observable<Models.Team[]> {
    var that = this;
    var urls = [];

    return this.makeGetRequest<Models.GetTeamsResponse>(`https://technossus.visualstudio.com/DefaultCollection/_apis/projects/${project.id}/teams`).map(response => {
        return response.value;
    }).catch(err => {
        return Observable.empty<Models.Team[]>();
    });
}

getAllCurrentIterations(teamId: string, projectId: string): Observable<Models.Iteration[]> {
    return this.makeGetRequest<Models.GetIterationsResponse>(`https://technossus.visualstudio.com/DefaultCollection/${projectId}/${teamId}/_apis/work/TeamSettings/Iterations?$timeframe=current`).map(response => {
        return response.value;
    }).catch((ex, caught) => {
        console.log('Couldn\'t get team');
        return Observable.from<Models.Iteration[]>({
            length: 0,
            value:[]
        });
    });
}

getIterationsDashboard() {
    var that = this;

    return this.getAllProjects().flatMap(projects => {
        let dashboard: Models.TeamSprintDashboard[] = [];


        return Observable.forkJoin(projects.map(project => {
            return that.getAllTeams(project).flatMap(teamsResponse => {

                return Observable.forkJoin(teamsResponse.map(team => {
                    return that.getAllCurrentIterations(team.id, project.id).map(iterations => {

                        return {
                            projectId: project.id,
                            teamId: team.id,
                            iterations: iterations ? iterations : []
                        };
                    }).catch(ex => {
                        console.log('no success');

                        return Observable.empty();
                    });
                })).catch(err => {
                    return Observable.empty();
                });
            });
        }));

    });
}

makeGetRequest<T>(requestUrl): Observable<T> {
    var authHeader = new Headers();
    authHeader.append('Authorization', `Basic ${new Buffer('k3ulyrj5xaid5tpr7l2hlhgtxeqynmvmlcwa4v47kn6l25eupqkq:').toString('base64')}`);
    authHeader.append('X-TFS-FedAuthRedirect', 'Suppress');

    return this.http.get(requestUrl, {
        headers: authHeader
    }).map(res => {
        let body: T = res.json();

        return body;
    }).catch(err => {
        return Observable.empty<T>();
    });
}

1 个答案:

答案 0 :(得分:1)

我认为catch应该做你想要的(需要导入):

return this.http.get(`${this.baseUrl}`, {
    headers: authHeader
}).map(res => {
    let body: Models.GetProjectsResponse = res.json();

    return body.value;
}).catch(err => Observable.of(null));