Skip to content

Latest commit

 

History

History
38 lines (31 loc) · 1.08 KB

error_handling.md

File metadata and controls

38 lines (31 loc) · 1.08 KB

Error Handling

If something unexpected arises we can raise an error on the Observable stream and use the function reserved for handling errors in our subscribe routine to see what happened.

export class App {

	private data: Observable<Array<number>>;
	private values: Array<number> = [];
	private anyErrors: error;

	constructor() {

		this.data = new Observable(observer => {
		  	setTimeout(() => {
				observer.next(10);
			}, 1500);
			setTimeout(() => {
				observer.error(new Error('Something bad happened!'));
			}, 2000);
			setTimeout(() => {
				observer.next(50);
			}, 2500);
		});

		let subscription = this.data.subscribe(
			value => this.values.push(value),
			error => this.anyErrors = error
		);
	}
}

View Example

Here an error is raised and caught. One thing to note is that if we included a .complete() after we raised the error, this event will not actually fire. Therefore you should remember to include some call in your error handler that will turn off any visual loading states in your application.