Angular 2 Tutorial
Angular 2 Useful Resources
Selected Reading
- Angular 2 - Services
- Angular 2 - Nested Containers
- Angular 2 - Lifecycle Hooks
- Angular 2 - User Input
- Angular 2 - Custom Pipes
- Angular 2 - Transforming Data
- Angular 2 - Handling Events
- Angular 2 - Data Display
- Angular 2 - Third Party Controls
- Angular 2 - Advanced Configuration
- Angular 2 - Dependency Injection
- Angular 2 - CLI
- Angular 2 - Forms
- Angular 2 - Navigation
- Angular 2 - Routing
- Angular 2 - Error Handling
- CRUD Operations Using HTTP
- Angular 2 - Data Binding
- Angular 2 - Metadata
- Angular 2 - Directives
- Angular 2 - Templates
- Angular 2 - Components
- Angular 2 - Architecture
- Angular 2 - Modules
- Angular 2 - Hello World
- Angular 2 - Environment
- Angular 2 - Overview
- Angular 2 - Home
Angular 2 Useful Resources
- Angular 2 - Discussion
- Angular 2 - Useful Resources
- Angular 2 - Quick Guide
- Angular 2 - Questions and Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Angular 2 - Error Handling
Angular 2 - Error Handpng
Angular 2 apppcations have the option of error handpng. This is done by including the ReactJS catch pbrary and then using the catch function.
Let’s see the code required for error handpng. This code can be added on top of the chapter for CRUD operations using http.
In the product.service.ts file, enter the following code −
import { Injectable } from @angular/core ; import { Http , Response } from @angular/http ; import { Observable } from rxjs/Observable ; import rxjs/add/operator/map ; import rxjs/add/operator/do ; import rxjs/add/operator/catch ; import { IProduct } from ./product ; @Injectable() export class ProductService { private _producturl = app/products.json ; constructor(private _http: Http){} getproducts(): Observable<IProduct[]> { return this._http.get(this._producturl) .map((response: Response) => <IProduct[]> response.json()) .do(data => console.log(JSON.stringify(data))) .catch(this.handleError); } private handleError(error: Response) { console.error(error); return Observable.throw(error.json().error()); } }
The catch function contains a pnk to the Error Handler function.
In the error handler function, we send the error to the console. We also throw the error back to the main program so that the execution can continue.
Now, whenever you get an error it will be redirected to the error console of the browser.
Advertisements