Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
how to handle observable errors but continue with follow on observables?

I have an observable, does an http get in the middle of processing other observables. In the case of the http get response code of anything but 200, I want to take note of this error but proceed to the next observable.

So far I have this:

this.getConfigurationSettings()
    .do(config => {
        console.log('configuration settings.: ', config);
        this.configSettings = config;
        this.subscriptionService.setWSAddressProvider('badUrl');
    })
    .switchMap(config => this.askForWSData())
    .do(config =>
        console.log('askForWSData' + config))
    .switchMap(r => this.processWSData())
    .subscribe(
        config => {
            console.log('start of data processing: ' + config);
        },
        err => {
            // Log errors if any
            console.log(err);
        },
        () => console.log('app exiting'));

and the observable that can return an http error code is the following:

setWSAddressProvider() : Observable<string[]> {
    return this.http.get('badUrl')
        .map((res:Response) => {
            this.address = res.text();
            return [res.text()];
        });
        // .catch((error:any) =>
        // Observable.throw('Server error')
        // );
}

the above case generate a 400 response code. I want to log that return but go on to other observables. How to do that?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use catch to handle http errors

setWSAddressProvider() : Observable<string[]> {
    return this.http.get('badUrl')
        .map((res:Response) => {
            this.address = res.text();
            return [res.text()];
        });
       .catch((error: Response | any) => {
           if (error instanceof Response) {
                if (error.status === 400) {
                    console.log("Server responded with 400");
                    // Create a new observable with the data for the rest of the chain
                    return Observable.of([]);
                }
           }
           // Re-throw unhandled error
           return Observable.throw(err);
    });

}

about 4 years ago · Santiago Trujillo Denunciar

0

Try using the Observable onErrorResumeNext function:

setWSAddressProvider() : Observable<string[]> {
    return this.http.get('badUrl')        
        .map((res:Response) => {
            this.address = res.text();
            return [res.text()];
        })
       .onErrorResumeNext((error: Response | any) => {
           if (error instanceof Response) {
                if (error.status === 400) {
                    console.log("Server responded with 400");
                }
                return [];
           }
    });
  }

See https://xgrommx.github.io/rx-book/content/getting_started_with_rxjs/creating_and_querying_observable_sequences/error_handling.html#ignoring-errors-with-onerrorresumenext

and this answer: Difference between catch and onErrorResumeNext

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda