简体   繁体   English

我可以在Angular2中的RXJS observable中的“subscribe()”之前捕获某些错误吗?

[英]Can I catch certain errors before “subscribe()” in an RXJS observable in Angular2?

Is it possible for a base class to catch certain errors before allowing the subclass to subscribe to the observable in Angular2. 在允许子类subscribe Angular2中的observable之前,基类是否有可能catch某些错误。

eg 例如

export class SomeBaseClass {
    constructor(private _http: Http, private _location: Location) {}

    protected _fetchData(url): Observable<any> {
        const headers = new Headers();
        headers.append('Authorization', 'Token foo');
        return this._http.get(url, {headers})
            .map(response => response.json())
            .catch(response => this._handle401(error));
    }

    private _handle401(response: Response) {
        if(response.status === 401) {
            this._location.go('/login');
        }

        // What should this return?
    }
}

export class SomeClass extends SomeBaseClass {
    constructor( _http: Http,  _location: Location) {
        super(_http, _location);
    }

    doTheThing() {
        this._fetchData('/someUrl')
            .subscribe(
                response => this._handleResponse(response),
                error => this._handleErrorThatIsNot401(error));
    }

    private _handleResponse(response) {
        // ...
    }

    private _handleErrorThatIsNot401(error) {
        // ...
    }
}

Is catch what I am looking for? catch我想要的东西? Should I be using map (or something else)? 我应该使用map (或其他东西)吗? Or am I going about this the wrong way entirely? 或者我完全以错误的方式解决这个问题?

Update 更新

Both answers (so far) put me on the right track, ultimately - I solved it like this: 这两个答案(到目前为止)都让我走上正轨,最终 - 我解决了这个问题:

protected _get(url: string, data?: any): Observable<any> {
    return super._get(url, data, this._authorizationHeader)
        .map(response => response.json())
        .catch(response => this._handle401(response));
}

private _handle401(response: Response): Observable<any> {
    try {
        if(response.status === 401) {
            this._router.navigateByUrl('/login');      
            return Observable.throw(response.status);  
        }
    } catch(err) {
        console.warn('AuthenticatedHttpService._handle401');
        console.error(err);
    }

    return Observable.of(response);
}

Using catch alone does not help much since you have client code subscribed and you must return Observable from catch . 单独使用catch并没有多大帮助,因为您已订阅客户端代码,并且必须从catch返回Observable

I would implement it as follows: 我会按如下方式实现它:

Rx.Observable.of(42)
.do(v=>{throw new Error('test')})
.catch(Rx.Observable.of(undefined))
.filter(v=>{return v !== undefined})
.subscribe(
(e)=>{console.log('next', e)}, 
(e)=>{console.log('error', e)}, 
()=>{console.log('complete')}
);

.catch() is the right one. .catch()是正确的。

Observable is lazy, so there are no errors before you subscribe. Observable是懒惰的,因此在您订阅之前没有错误。 Not sure if you mean this kind of "before" therefore I mention it just to be sure. 不确定你是否意味着这种“之前”因此我提到它只是为了确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM