Angular - Ngrx - 在 http-status 0 处重试服务器请求

发布于 2025-01-13 23:41:41 字数 1639 浏览 2 评论 0原文

设置

  • Angular 13.2.0
  • Ngrx 13.0.2
  • 后端:Spring Boot

目标

当后端未运行时,我想重试 url x 次。

到目前为止有效的

fetchEntities$ = createEffect(() =>
    this.actions$.pipe(
      ofType(DoctransActions.fetchEntities),
      exhaustMap(action =>
        this.http.get<DoctransResponse>(`${this.apiUrl}/some url`).pipe(
          map(response => {
            return action to set entities
          }),
          catchError((err) => {
            return action for error
        )
      )
    )
  );

拦截器

@Injectable()
export class RetryInterceptor implements HttpInterceptor {

  constructor(private store: Store<AppState>) {}

  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    return next.handle(request).pipe(
      retryWhen(error =>
        error.pipe(
          concatMap((error, count) => {
            if (count < environment.http.retry && environment.http.retryAtCodes.includes(error.status)) {
              this.store.dispatch(retryFetch({errorRespone: error, retryCount: (count+1)}));
              return of(error);
            }
            return throwError(error);
          }),
          delay(environment.http.delay)
        )
      )
    )
  }
}

retryFetch 操作为系统尝试连接的用户设置一个错误。

问题

有更好的方法吗?

解决方案

请参阅: Angular - ngrx - 持续从服务器获取数据的效果

EvenSource 会自动重新连接

Setup

  • Angular 13.2.0
  • Ngrx 13.0.2
  • Backend: Spring Boot

Goal

When the backend is not running i want to retry the url x-times.

What works so far

fetchEntities$ = createEffect(() =>
    this.actions$.pipe(
      ofType(DoctransActions.fetchEntities),
      exhaustMap(action =>
        this.http.get<DoctransResponse>(`${this.apiUrl}/some url`).pipe(
          map(response => {
            return action to set entities
          }),
          catchError((err) => {
            return action for error
        )
      )
    )
  );

Interceptor

@Injectable()
export class RetryInterceptor implements HttpInterceptor {

  constructor(private store: Store<AppState>) {}

  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    return next.handle(request).pipe(
      retryWhen(error =>
        error.pipe(
          concatMap((error, count) => {
            if (count < environment.http.retry && environment.http.retryAtCodes.includes(error.status)) {
              this.store.dispatch(retryFetch({errorRespone: error, retryCount: (count+1)}));
              return of(error);
            }
            return throwError(error);
          }),
          delay(environment.http.delay)
        )
      )
    )
  }
}

The retryFetch Action sets an error for the user that the system is trying to connect.

Question

Is there a better way of doing this?

Solution

See: Angular - ngrx - Effect for continually getting data from server

EvenSource does automatic reconnecting

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文