Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular:如何检查拦截器抛出后的类类型_Angular_Typescript_Rxjs - Fatal编程技术网

Angular:如何检查拦截器抛出后的类类型

Angular:如何检查拦截器抛出后的类类型,angular,typescript,rxjs,Angular,Typescript,Rxjs,我想检查来自拦截器执行throwError的错误类类型。(错误类类型意味着类实现了错误。) 但是,当我发现这个错误时,它会在另一种错误类型的情况下工作。 (在本例中,我希望将错误检查为MyCustomError1,但尽管MyCustomError2仍然有效。) 注意:MyCutomError1和MyCutomError2实现相同的错误类 那么,我应该如何适当地检查类型呢 我的拦截器在这里 @Injectable() export class ErrorInterceptor implements

我想检查来自拦截器执行throwError的错误类类型。(错误类类型意味着类实现了错误。)

但是,当我发现这个错误时,它会在另一种错误类型的情况下工作。 (在本例中,我希望将错误检查为MyCustomError1,但尽管MyCustomError2仍然有效。)

注意:MyCutomError1和MyCutomError2实现相同的错误类

那么,我应该如何适当地检查类型呢

我的拦截器在这里

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        const _req = req.clone();
        return next.handle(_req)
            .pipe(
                catchError((errRes: HttpErrorResponse) => {
                    let _error: Error = null;
                    switch (errRes.status) {
                        case 401:
                            _error = new MyCustomError1(errRes.name, errRes.message, errRes.error);
                            break;
                        case 503:
                            _error = new MyCustomError2(errRes.name, errRes.message, errRes.error);
                        default:
                            _error = errRes;
                            break;
                    }
                    return throwError(_error);
                })
            );
    }
}

这可能会有帮助,谢谢你,我不明白。这很有帮助。但是,我真正想知道的是区分一个错误类和另一个错误类。我修改了我的帖子。如果您有空,请再次检查。对不起,我错过了第二个链接。当我试图通过构造函数来比较对象时,它似乎起了作用。我将学习打字。也许我的问题解决了。谢谢。顺便说一下,这不是Typescript,而是Javascript规范。非常感谢。是的,这是js。希望你已经解决了这个问题
this.myService.doSomething()
  .pipe(
        catchError((err: Error) => {

        // How should I check the type?
        // In the case of MyCustomError2, this returns true.
        if(err instanceof MyCustomError1){
          // error handling
        }
        return EMPTY;
      })
   ).subscribe(()=>{})