gpt4 book ai didi

angular - HttpInterceptors 处理 http 错误 Angular 4.3

转载 作者:太空狗 更新时间:2023-10-29 19:28:39 25 4
gpt4 key购买 nike

我正在尝试通过简单的 http get 使用 rest api。当没有注册时,我的 api 会响应这样的错误 500:

{ "errors": [ { "code": "500", "message": "no registers."} ]}

所以,我想知道如何编写一个拦截器来处理所有类型的 http 错误,以防止在浏览器的控制台记录错误。

我的 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 {
}

我的 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.`);
}
});

}
}

我的 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 是调用 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

最佳答案

实际上不可能实现你想要的,只是浏览器的默认行为,见jeff的回复@angular的github:

https://github.com/angular/angular/issues/8832

关于angular - HttpInterceptors 处理 http 错误 Angular 4.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45200919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com