One of the most often cited problems with Promises is that if you don't explicitly handle an error condition in a Promise chain, it's easy to accidentally swallow said error, hiding it from both process and logging. RxJS Observable streams, on the other hand, seem to take a completely different approach. In an RxJS Observable stream, if you don't handle an error condition explicitly, RxJS will handle it implicitly by throwing your error. In Angular 2 Beta 6, the RxJS stream is being managed (so to speak) by the core Zone.js instance which means that these unhandled errors are subsequently caught by the Angular 2 default ExceptionHandler service.

To explore this RxJS feature, I put together a tiny little demo that does nothing but create an "error stream" without an error handler (ie, a subscriber that doesn't listen for error events). I also added a few error handling and error conversion functions that you can see in the video:

.throw( new Error( "Oops: I Did It Again." ) ) // I played with your heart, got lost in the game.

.subscribe(

function handleValue( value ) {

console.log( "handleValue:", value );

}

// NOTE: Uncomment this to handle the upstream error

// and prevent the error from being thrown.

// --

// , handleError

// Notice that we are not providing a CATCH handler in

// our subscriber configuration. As such, the underlying

// Subscriber class is automatically providing one for

// us, which basically implements:

// --

// function handleError( e ) { throw( e ); }

)

;

// I log errors.

function handleError( error ) {

console.warn( "Caught an error in the stream." );

console.log( error );

}

}

}

}

);

</script>

</body>

</html>

As you can see, we're using the .throw() operator to start an error stream; but, we're not catching or handling said error. So, when we run this code and click the link, we get the following console output:

As you can see, the unhandled error was rethrown (for lack of a better term) by RxJS. This thrown error was then caught by Zone.js and piped back into the Angular 2 context where it was subsequently logged by the ExceptionHandler service.

Coming from a Promise background, this behavior kind of caught me by surprise. But, I think this is a really nice feature of RxJS Observable streams. It means that unexpected error conditions will never get lost in an Angular 2 application. Well, not if you're using streams.

I am the co-founder and lead engineer at InVision App, Inc — the world's leading prototyping,
collaboration & workflow platform. I also rock out in JavaScript and ColdFusion 24x7 and I dream about
promise resolving asynchronously.