Error handling with Angular2 async pipe

user663031

I am using the Angular2 async pipe to stream values into the DOM. Here's a real simple example:

const stream = Observable.interval(1000)
  .take(5)
  .map(n => { if (n === 3) throw "ERROR"; return n; });

<div *ngFor="for num of stream | async">
  {{num}}
</div>

<div id="error"></div>

What I would like to do is to have the sequence of 1-5 displayed, but on the error item (3), somehow populate the #error div with the error message.

This seems to require two things: first is the ability of the Angular async pipe to do something intelligent with errors, which I see no sign of. Looking at the source code, apparently it throws a JS exception, which doesn't seem too friendly.

Second is the ability to restart or continue the sequence after the error. I have read about catch and onErrorResumeNext and so on, but they all involve another sequence which will be switched to on an error. This greatly complicates the logic of generating the stream, on which I would just like to put a series of numbers (in this simple example). I have the sinking feeling that once an error occurs the game is over and the observable is completed and can only be "restarted" with a different observable. I'm still learning observables; is this in fact the case?

So my question is twofold:

  1. Can Angular2's async pipe do something intelligent with errors?
  2. Do observables have some simple way to continue after an error?
Thierry Templier

Yes you're right regarding the catch operator and the ability to do something after errors occur...

I would leverage the catch operator to catch the error and do something:

const stream = Observable.interval(1000)
  .take(5)
  .map(n => {
    if (n === 3) {
      throw Observable.throw(n);
    }
    return n;
  })
  .catch(err => {
    this.error = error;
    (...)
  });

and in the template:

<div>{{error}}</div>

To be able to go on the initial observable, you need to create a new one starting at the point where the error occurs:

createObservable(i) {
  return Observable.interval(1000)
    .range(i + 1, 5 - i)
    .take(5 - i)
  });
}

and use it in the catch callback:

  .catch(err => {
    this.error = error;
    return this.createObservable(err);
  });

These two questions could help you:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

angular2 - pipe error

From Dev

Angular2 async pipe with observable

From Dev

Angular2 : HTTP error handling

From Dev

Error Handling - Async Call

From Dev

Jasmine async test error handling

From Dev

Angular async pipe and object property

From Dev

spyOn not working with async pipe in Angular

From Dev

Angular 2 using ngFor with the async pipe and index

From Dev

Cannot find a differ supporting object ,Async pipe and ngFor problems in angular2

From Dev

How to consume single Observable property in view from Angular2 component using async pipe?

From Dev

Error in Async pipe while using autocomplete

From Dev

Async/future error handling in Dart not working as expected

From Dev

Error handling in Spring integration flow async

From Dev

Error handling for async functions without callback or promises

From Dev

How to do proper error handling with async/await

From Dev

Handling error from Async Task in an Async ASP.Net page

From Java

How to handle boolean observables with the async pipe in Angular

From Dev

The pipe 'async' could not be found - Angular 11

From Dev

Angular cannot use async pipe with my observables

From Dev

The pipe 'async' could not be found - Angular 11

From Dev

Error value has changed after checked (Stateful Pipe in Angular2)

From Dev

Angular2 pipe causing '...has changed after it was checked' dev error

From Dev

Angular2: Error while using a text filter pipe to filter an array

From Dev

Angular 2 async pipe not rendering/updating Observable data automatically

From Java

Using an array from Observable Object with ngFor and Async Pipe Angular 2

From Dev

Safe navigation operator on an async observable and pipe in Angular 2

From Dev

Angular $http not handling error

From Dev

Error handling in Angular with injection

From Dev

Handling 404 with Angular2

Related Related

  1. 1

    angular2 - pipe error

  2. 2

    Angular2 async pipe with observable

  3. 3

    Angular2 : HTTP error handling

  4. 4

    Error Handling - Async Call

  5. 5

    Jasmine async test error handling

  6. 6

    Angular async pipe and object property

  7. 7

    spyOn not working with async pipe in Angular

  8. 8

    Angular 2 using ngFor with the async pipe and index

  9. 9

    Cannot find a differ supporting object ,Async pipe and ngFor problems in angular2

  10. 10

    How to consume single Observable property in view from Angular2 component using async pipe?

  11. 11

    Error in Async pipe while using autocomplete

  12. 12

    Async/future error handling in Dart not working as expected

  13. 13

    Error handling in Spring integration flow async

  14. 14

    Error handling for async functions without callback or promises

  15. 15

    How to do proper error handling with async/await

  16. 16

    Handling error from Async Task in an Async ASP.Net page

  17. 17

    How to handle boolean observables with the async pipe in Angular

  18. 18

    The pipe 'async' could not be found - Angular 11

  19. 19

    Angular cannot use async pipe with my observables

  20. 20

    The pipe 'async' could not be found - Angular 11

  21. 21

    Error value has changed after checked (Stateful Pipe in Angular2)

  22. 22

    Angular2 pipe causing '...has changed after it was checked' dev error

  23. 23

    Angular2: Error while using a text filter pipe to filter an array

  24. 24

    Angular 2 async pipe not rendering/updating Observable data automatically

  25. 25

    Using an array from Observable Object with ngFor and Async Pipe Angular 2

  26. 26

    Safe navigation operator on an async observable and pipe in Angular 2

  27. 27

    Angular $http not handling error

  28. 28

    Error handling in Angular with injection

  29. 29

    Handling 404 with Angular2

HotTag

Archive