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

reza

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?

Ofer Herman

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

}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to handle multiple errors in go?

分類Dev

Alamofire: how to handle and classify errors?

分類Dev

How to handle navigation with observables using Rx-MVVM-C

分類Dev

How to handle async Start() errors in TopShelf

分類Dev

How to catch or handle CUDA kernel launch errors

分類Dev

How to handle errors from Stored Procedure calls

分類Dev

How to handle errors with Bash arithmetic operations?

分類Dev

How to handle errors from Quickblox iOS SDK?

分類Dev

How do I continue even when a function errors?

分類Dev

How to merge parallel processing Observables while grouping items from the same Observable together

分類Dev

How can i limit a Knockout foreach on an observable array to 5 at a time without using computed observables?

分類Dev

How to handle low-level net/http errors?

分類Dev

How to handle errors in celery's Task.map

分類Dev

How do I handle errors in an f-string?

分類Dev

How to handle all possible errors cleanly when using HTTP conduit?

分類Dev

How should I handle an errors from server in Ember.js?

分類Dev

How to handle all errors from Python multiple pools?

分類Dev

How to handle errors related to sys.argv[] in python ?

分類Dev

Handle priority in two observable

分類Dev

Angular4 returning Observable with nested Observables

分類Dev

rx java observable chaining on only matched observables

分類Dev

Update statements - capture errors and continue with the rest

分類Dev

Handle errors using route resolver

分類Dev

Handle jwt auth errors in laravel

分類Dev

express jade handle helper errors

分類Dev

Angular5 HTTP service with Indexeddb as an offline datastore: how to handle Observable<response> vs Object

分類Dev

How do I handle errors for fs.createReadStream() in Node.js?

分類Dev

Angular 2 Observables - Return original observable, instead of switchMap/combineLatest

分類Dev

RxSwift - How to concatenate observables of collection

Related 関連記事

  1. 1

    How to handle multiple errors in go?

  2. 2

    Alamofire: how to handle and classify errors?

  3. 3

    How to handle navigation with observables using Rx-MVVM-C

  4. 4

    How to handle async Start() errors in TopShelf

  5. 5

    How to catch or handle CUDA kernel launch errors

  6. 6

    How to handle errors from Stored Procedure calls

  7. 7

    How to handle errors with Bash arithmetic operations?

  8. 8

    How to handle errors from Quickblox iOS SDK?

  9. 9

    How do I continue even when a function errors?

  10. 10

    How to merge parallel processing Observables while grouping items from the same Observable together

  11. 11

    How can i limit a Knockout foreach on an observable array to 5 at a time without using computed observables?

  12. 12

    How to handle low-level net/http errors?

  13. 13

    How to handle errors in celery's Task.map

  14. 14

    How do I handle errors in an f-string?

  15. 15

    How to handle all possible errors cleanly when using HTTP conduit?

  16. 16

    How should I handle an errors from server in Ember.js?

  17. 17

    How to handle all errors from Python multiple pools?

  18. 18

    How to handle errors related to sys.argv[] in python ?

  19. 19

    Handle priority in two observable

  20. 20

    Angular4 returning Observable with nested Observables

  21. 21

    rx java observable chaining on only matched observables

  22. 22

    Update statements - capture errors and continue with the rest

  23. 23

    Handle errors using route resolver

  24. 24

    Handle jwt auth errors in laravel

  25. 25

    express jade handle helper errors

  26. 26

    Angular5 HTTP service with Indexeddb as an offline datastore: how to handle Observable<response> vs Object

  27. 27

    How do I handle errors for fs.createReadStream() in Node.js?

  28. 28

    Angular 2 Observables - Return original observable, instead of switchMap/combineLatest

  29. 29

    RxSwift - How to concatenate observables of collection

ホットタグ

アーカイブ