当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Observable.catch方法代码示例

本文整理汇总了TypeScript中rxjs/Rx.Observable.catch方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Observable.catch方法的具体用法?TypeScript Observable.catch怎么用?TypeScript Observable.catch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rxjs/Rx.Observable的用法示例。


在下文中一共展示了Observable.catch方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: withRetry

 /**
  * This function handles automatic retry when 401 error happens.
  * The returned Observable throws errors with user friendly messages.
  */
 private withRetry(initialResult: Observable<Response>, func: (...params: any[]) => Observable<Response>, ...params: any[]): Observable<Response> {
     return initialResult
         .catch(error => {
             if (error.status === 401) {
                 this.authService.unauthenticated();     // 401 means authentication required
                 let message = this.authService.isOrgApiKeySet ? 'Incorrect organization API key' : 'Authentication is required';
                 return Observable.fromPromise(this.authService.login(message))
                     .mergeMap(confirmed => {
                         if (confirmed) { // logged in again, or updated api key
                             this.authService.authenticationMayChange();
                             switch (params.length) {
                                 case 1:
                                     return func(params[0]);
                                 case 2:
                                     return func(params[0], params[1]);
                                 case 3:
                                     return func(params[0], params[1], params[2]);
                                 default:
                                     return Observable.throw(new Error('Wrong number of parameters: ' + params));
                             }
                         }else {      // canceled
                             return Observable.throw(new Error('User cancelled authentication'));
                         }
                     });
             }else {
                 let errorDetail = error.json();
                 if (errorDetail) {
                     this.authService.authenticated();  // a valid API response received
                     return Observable.throw(new Error(errorDetail.type + ': ' + errorDetail.message));
                 }
                 console.log('API call failed: ' + JSON.stringify(error));
                 return Observable.throw(error);
             }
         });
 }
开发者ID:james-hu,项目名称:rtstatistics-web-mconsole,代码行数:39,代码来源:api-http.service.ts

示例2: intercept

  intercept(observable: Observable<Response>): Observable<Response> {
    return observable.catch((err, source) => {
      if (err.status  === 401 && !_.endsWith(err.url, '/login')) {
          this._router.navigate(['login']);
          return Observable.empty(null);
        } else {
          return Observable.throw(err);
      }
    });

  }
开发者ID:divino,项目名称:marklogic-data-hub,代码行数:11,代码来源:http.ts

示例3: constructor

    /**
     * Creates a new Reactive Command.
     * @param canRun An observable that determines whether the given task is allowed to run at a given moment.
     * @param task A function that returns an observable that represents the asynchronous operation.
     * @param scheduler The scheduler that all of the results should be observed on.
     */
    constructor(private task: (args: TArgs) => Observable<TResult>, private canRun: Observable<boolean>, private scheduler: Scheduler) {
        if (!task) {
            throw new Error("The task parameter must be supplied");
        }
        if (!canRun) {
            throw new Error("The canRun parameter must be supplied");
        }
        if (!scheduler) {
            throw new Error("The scheduler parameter must be supplied");
        }
        this._executionInfo = new Subject<ExecutionInfo<TResult>>();
        this._synchronizedExcecutionInfo = this._executionInfo;
        this._exceptions = new Subject<any>();

        // Implementation mostly taken from:
        // https://github.com/reactiveui/ReactiveUI/blob/rxui7-master/ReactiveUI/ReactiveCommand.cs#L628
        
        this._isExecuting = this._synchronizedExcecutionInfo
            .observeOn(scheduler)
            .map(info => info.demarcation === ExecutionDemarcation.Begin)
            .startWith(false)
            .distinctUntilChanged()
            .publishReplay(1)
            .refCount();
        this._canExecute = this.canRun
            .catch(ex => {
                this._exceptions.next(ex);
                return Observable.of(false);
            })
            .startWith(false)
            .combineLatest(this._isExecuting, (canRun, isExecuting) => {
                return canRun && !isExecuting;
            })
            .distinctUntilChanged()
            .publishReplay(1)
            .refCount();
        this._results = this._synchronizedExcecutionInfo
            .observeOn(scheduler)
            .filter(info => info.demarcation === ExecutionDemarcation.EndWithResult)
            .map(info => info.result);

        // Make sure that can execute is triggered to be a hot observable.
        this._canExecuteSubscription = this._canExecute.subscribe();
    }
开发者ID:KallynGowdy,项目名称:RxUI,代码行数:50,代码来源:reactive-command.ts

示例4: _handle

 private _handle(observable: Observable<Response>, url: string, options?: RequestOptionsArgs): Observable<any> {
   return observable.catch((err: any): any => {
     console.log(err);
     if (err.status === 400 || err.status === 401 || err.status === 422) {
       console.log("Notifying...");
       console.log(err.json());
       this.growlMessageService.notifyError(err.json());
       return Observable.empty();
     }
     else {
       console.log("Redirecting...");
       this.router.navigate(['/error-page']);
       return Observable.empty();
     }
   })
     .retryWhen(error => error.delay(500))
     .timeout(2000, new Error('delay exceeded'))
     .finally(() => {
       this._afterCall(url, options);
     });
 }
开发者ID:NahuelOlgiati,项目名称:ng2-authentication,代码行数:21,代码来源:custom-http.http.ts


注:本文中的rxjs/Rx.Observable.catch方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。