Mejores prácticas para el manejo de errores en Angular2


Hola estoy tratando de recibir un error enviado desde catch block (servicio). en varios componentes necesito mostrar una ventana emergente con el mensaje de error mostrado. por favor, hágame saber cómo crear un método genérico y llamarlo inside service block. Como lo que estoy haciendo ahora con"showErrorPage ()".

import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map'

@Injectable()
export class DataService {
    private reqData = {};
    private url: string;
    constructor(private http: Http) {
    }

    getResult(searchObject: {}): Observable<Response> {
        // some logic
        return this.http.post(<my url>, <data to be sent>)

        .map((response: Response) => {
            return response;
        })
        .catch((error: any) => {
            if (error.status === 302 || error.status === "302" ) {
                // do some thing
            }
            else {
              return Observable.throw(new Error(error.status));
            }
        });
    }
}

Y en mi componente lo estoy llamando como

import { Component,EventEmitter, Output, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
// importing DataService ';

@Component({
  selector: 'o-result',
  templateUrl: './o-result.component.html',
})

export class AComp implements OnInit {
    constructor(
        private dataService: DataService
    ){

    }

    ngOnInit() {
      this.dataService.getResult(<url>, <params>)
      .subscribe(
         response => {
             // doing logic with responce
         }
          ,
          error => {
            this.showErrorPage();
         }
      )

    }

    showErrorPage(): void {
      // displaying error in popup
    }
}
Author: Vaibhav, 2017-02-02

1 answers

De acuerdo con la guía de estilo angular

Los detalles de la administración de datos, como encabezados, métodos HTTP, almacenamiento en caché, manejo de errores y lógica de reintento, son irrelevantes para los componentes y otros consumidores de datos.

Su implementación parece ser la correcta.

Además, la documentación del cliente http proporciona la misma implementación.

 16
Author: JEY,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-10 11:58:59