J'essaie de consommer une api de repos, avec un http simple. Quand il n'y a pas de registres ma réponse api une erreur 500 comme ça:HttpInterceptors gérer les erreurs HTTP angulaire 4.3
{ "errors": [ { "code": "500", "message": "no registers." } ]}
Alors, je me demande comment je peux écrire un intercepteur pour gérer toutes sortes d'erreur HTTP pour empêcher la connexion de la console de l'erreur @ navigateur.
Mes app.module.ts
import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { sharedConfig } from './app.module.shared';
import 'rxjs/add/operator/toPromise';
import { NoopInterceptor } from "./app.interceptor";
@NgModule({
bootstrap: sharedConfig.bootstrap,
declarations: sharedConfig.declarations,
imports: [
BrowserModule,
FormsModule,
HttpClientModule,
...sharedConfig.imports
],
providers: [
{ provide: 'ORIGIN_URL', useValue: location.origin },
{
provide: HTTP_INTERCEPTORS,
useClass: NoopInterceptor,
multi: true,
}
]
})
export class AppModule {
}
Mes app.interceptor.ts
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable } from "rxjs/Observable";
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/map';
import { HttpErrorResponse } from "@angular/common/http";
@Injectable()
export class NoopInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const started = Date.now();
return next.handle(req)
.do(event => {
debugger;
if (event instanceof HttpResponse) {
const elapsed = Date.now() - started;
console.log(`Request for ${req.urlWithParams} took ${elapsed} ms.`);
}
});
}
}
Mes app.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { Config } from "./app.constantes";
@Injectable()
export class AppService {
protected config: Config;
constructor(private http: HttpClient) {
this.config = new Config();
}
get(service: string, complementos?: any, parametros?: any) {
var complemento = complementos != null && complementos.length > 0 ? complementos.join('/') : '';
var url = this.config.SERVER + service + this.config.TOKEN + '/' + complemento;
return this.http.get(this.config.SERVER + service + this.config.TOKEN + '/' + complemento);
}
}
compra.component.ts est là un faire mon appel get
consultaPeriodoCompra(mes: any): void {
var lista = null;
this.service.get(this.config.CONSULTA_ULTIMAS_COMPRAS, ['2', mes.anoMes])
.subscribe((response) => {
this.atualizaLista(mes, response['payload'].listaTransacao);
},
(err) => {
this.atualizaLista(mes, []);
});
}
that it's what i want to prevent
Est-ce que vous retournez aussi le code de statut 500? Ou il renvoie un 200 avec lesdites erreurs? – LookForAngular
il retourne également le code d'état 500. –
Que voulez-vous dire en empêchant la connexion dans le navigateur? Est-ce votre seul but? – LookForAngular