Angular 2 在派生中捕获可观察到的 http 错误 class
Angular 2 catch observable http error in derived class
我想在我的全局异常处理程序中捕获 HTTP 错误。
异常处理程序适用于大多数异常,但不会捕获可观察到的异常。我要捕获的异常是 HTTP 异常。
这就是我尝试将 HTTP 可观察错误发送到异常处理程序的方式。
import { Injectable } from '@angular/core';
import { Request, XHRBackend, RequestOptions, Http, Response, RequestOptionsArgs } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { HttpException } from '../exceptions/http-exception';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class HttpErrorService extends Http {
constructor(backend: XHRBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs) {
return super.request(url, options).catch((error: Response) => {
// Bypass lint.
fail();
function fail() {
// Here I want to throw the exception to send it to the exception handler, but it should also execute return Observable.throw(error); So I can catch the exception at the subscribe.
throw new HttpException(error);
}
return Observable.throw(error);
});
}
}
这当然是行不通的,因为throw之后的代码没有被执行。
但是抛出的异常也没有被捕获,可能是因为这是在 observable 中完成的。
有没有办法在全局异常处理程序中捕获异常,并且请求在subscribe((res) => {}, (errRes) => {/*here*/})
仍然可用?
您需要 return 从头开始创建新的可观察对象:
@Injectable()
export class HttpErrorService extends Http {
constructor(backend: XHRBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs) {
return Observable.create(observer => {
super.request(url, options).subscribe(
res => observer.next(res), //simply passing success response
err => { //error handling
console.log("global error handler");
observer.error(err); //passing error to the method which invoked request
},
() => observer.complete() //passing onComplete event to the method which invoked request
);
});
}
}
您可以简单地注入全局错误处理程序并对其调用 handleError:
export class HttpErrorService extends Http {
constructor(errorHandler: ErrorHandler, backend: XHRBackend...) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs) {
return super.request(url, options).catch((error: Response) => {
// Bypass lint.
fail();
function fail() {
// Here I want to throw the exception...
const error = new HttpException(error);
errorHandler.handleError(error); //<----------------- here!
}
return Observable.throw(error);
});
}
}
我想在我的全局异常处理程序中捕获 HTTP 错误。
异常处理程序适用于大多数异常,但不会捕获可观察到的异常。我要捕获的异常是 HTTP 异常。
这就是我尝试将 HTTP 可观察错误发送到异常处理程序的方式。
import { Injectable } from '@angular/core';
import { Request, XHRBackend, RequestOptions, Http, Response, RequestOptionsArgs } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { HttpException } from '../exceptions/http-exception';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class HttpErrorService extends Http {
constructor(backend: XHRBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs) {
return super.request(url, options).catch((error: Response) => {
// Bypass lint.
fail();
function fail() {
// Here I want to throw the exception to send it to the exception handler, but it should also execute return Observable.throw(error); So I can catch the exception at the subscribe.
throw new HttpException(error);
}
return Observable.throw(error);
});
}
}
这当然是行不通的,因为throw之后的代码没有被执行。
但是抛出的异常也没有被捕获,可能是因为这是在 observable 中完成的。
有没有办法在全局异常处理程序中捕获异常,并且请求在subscribe((res) => {}, (errRes) => {/*here*/})
仍然可用?
您需要 return 从头开始创建新的可观察对象:
@Injectable()
export class HttpErrorService extends Http {
constructor(backend: XHRBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs) {
return Observable.create(observer => {
super.request(url, options).subscribe(
res => observer.next(res), //simply passing success response
err => { //error handling
console.log("global error handler");
observer.error(err); //passing error to the method which invoked request
},
() => observer.complete() //passing onComplete event to the method which invoked request
);
});
}
}
您可以简单地注入全局错误处理程序并对其调用 handleError:
export class HttpErrorService extends Http {
constructor(errorHandler: ErrorHandler, backend: XHRBackend...) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs) {
return super.request(url, options).catch((error: Response) => {
// Bypass lint.
fail();
function fail() {
// Here I want to throw the exception...
const error = new HttpException(error);
errorHandler.handleError(error); //<----------------- here!
}
return Observable.throw(error);
});
}
}