Announcement

👇Official Account👇

Welcome to join the group & private message

Article first/tail QR code

Skip to content

Lesson 5.3: 错误处理

学习目标

  • 掌握 RxJS 的错误处理策略

1. 错误处理操作符

typescript
import { catchError, retry, retryWhen, delay, take } from 'rxjs/operators';

// catchError:捕获错误,返回备用 Observable
http$.pipe(
    catchError(err => {
        console.error('Request failed:', err);
        return of(defaultData); // 降级数据
    })
);

// retry:失败后重试 N 次
http$.pipe(
    retry(3) // 最多重试 3 次
);

// retryWhen:自定义重试策略
http$.pipe(
    retryWhen(errors =>
        errors.pipe(
            delay(1000),      // 每次重试等待 1s
            take(3)           // 最多重试 3 次
        )
    )
);

2. 策略选择

场景推荐策略说明
网络抖动retry(3)简单重试
表单提交retryWhen + delay指数退避
数据降级catchError使用缓存/默认值
致命错误不捕获向上传播错误

上次更新于: