Chris Woolum
Chris Woolum

Reputation: 2904

Ignoring errors with flatMap using RXJS

I'm trying to use flatmap to make a bunch of parallel requests. The problem is though that if I get a 404, the whole chain breaks and the remainder of the requests are cancelled. Is there any way to catch the 404 and return an empty response? getIterationsDashboard is the initial method I run.

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>();
    });
}

Upvotes: 2

Views: 1247

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657118

I think catch should do what you want (needs to be imported):

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

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

Upvotes: 1

Related Questions