• Technologies
  • Frontend
  • Backend
  • Server
  • Contact
  • About
Friday, May 9, 2025
Adnan Halilovic Blog
  • Technologies
    • All
    • Angular
    • Git
    • JavaScript
    • ReactJS
    VS Code Setup for Angular Developers Featured Image

    Best VS Code Setup for Angular Development

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Quokka.js and Wallaby.js Giveaway

    Quokka.js and Wallaby.js Giveaway

    How to Work on Different Git Branches at the Same Time?

    How to Work on Different Git Branches at the Same Time?

    Lazyload Standalone Components in Angular

    Lazyloading Standalone Components in Angular

    Standalone Components Angular

    How to Create Standalone Components in Angular

    Private Routes in React Router V6

    Protecting Routes in React JS

    How to setup routing in ReactJS

    How to setup routing in ReactJS

    Retry Error HTTP Requests in Angular (without retryWhen)

    Retry Error HTTP Requests in Angular (without retryWhen)

  • Frontend
  • Backend
  • Server
  • Contact
  • About
No Result
View All Result
  • Technologies
    • All
    • Angular
    • Git
    • JavaScript
    • ReactJS
    VS Code Setup for Angular Developers Featured Image

    Best VS Code Setup for Angular Development

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Quokka.js and Wallaby.js Giveaway

    Quokka.js and Wallaby.js Giveaway

    How to Work on Different Git Branches at the Same Time?

    How to Work on Different Git Branches at the Same Time?

    Lazyload Standalone Components in Angular

    Lazyloading Standalone Components in Angular

    Standalone Components Angular

    How to Create Standalone Components in Angular

    Private Routes in React Router V6

    Protecting Routes in React JS

    How to setup routing in ReactJS

    How to setup routing in ReactJS

    Retry Error HTTP Requests in Angular (without retryWhen)

    Retry Error HTTP Requests in Angular (without retryWhen)

  • Frontend
  • Backend
  • Server
  • Contact
  • About
No Result
View All Result
Adnan Halilovic Blog
No Result
View All Result

Retry Error HTTP Requests in Angular (without retryWhen)

Adnan Halilovic by Adnan Halilovic
September 7, 2022
in Angular, Frontend, Technologies
0
0
SHARES
4.8k
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

Let’s say you want to retry error requests in Angular and aren’t sure how to do it. If you have used the retryWhen operator, which will be removed from future versions of RxJS, this article is also for you.


I will assume that you already have created a project with the ng new command in the Angular CLI.

So, to be able to retry requests, first of all, we have to know which requests to retry and somehow catch them. To do that we will need to use an Angular Interceptor. I will create an interceptor named retry in the core/interceptors folder by running the following command:

ng g i core/interceptors/retry

After running the command, our folder structure should look like this:

Retry Interceptor Folder Structure

And our retry.interceptor.ts should have the following code:

ADVERTISEMENT
import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpInterceptor,
  HttpErrorResponse,
} from '@angular/common/http';
import { Observable, retry, timer } from 'rxjs';

@Injectable()
export class RetryInterceptor implements HttpInterceptor {
  constructor() {}


  intercept(
    request: HttpRequest<unknown>,
    next: HttpHandler
  ): Observable<HttpEvent<unknown>> {
    return next.handle(request)
  }
}

What do we have to do here?

So, by default we would pipe the return request and use retryWhen operator from RxJS. But, as the retryWhen is going to be deprecated as of version 9 or 10, we will use the suggested error operator from RxJS – retry. Hmm… how to use the retry to retry when an error occurs? – Easy. Watch me. 🙂

I will make a function that checks if it should retry or not, or maybe it would be better to say that I will make a function that retries requests with certain status codes.

ADVERTISEMENT
shouldRetry(error: HttpErrorResponse){
    if(error.status >= 500){
      return timer(1000);
    }
    throw error;
  }

The next step is to add the retry operator to the cought request.

intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    return next.handle(request).pipe(retry({count: 2, delay: this.shouldRetry}));
  }

Let me explain what just happened.

As the retry operator is able to receive an object of properties, we are adding count – that counts for how many times the request should be retried. And also, we are adding the delay property, that fires the retry after the given period of time. But, there is a catch. The delay is expecting an observable, not just a number.

So to add it, we have used the timer operator from RxJS. Also, as you could mention, we are retrying only server errors (status codes 5xx).

Importing interceptor into app module

To be able to use our interceptor, we have to import it into our app module somehow. So here is a piece of code that as to be in the app.module.ts .

Ps. take a look at the providers array.

import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';
import { RetryInterceptor } from './core/interceptors/retry.interceptor';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, HttpClientModule],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: RetryInterceptor,
      multi: true,
    },
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

How to test it?

To test this, you just have to make an http request to an API that returns an error with number 500 or more.

I will help you with that as well. Here is a website that is actually doing just that, returning specific status codes using an API.

https://httpstat.us – you are welcome 🙂

Conclusion

Retrying error requests using retry operator seems shorter and more readable than using retryWhen (in my opinion) and it is really easy to implement.

You can also find the full code on my github page, or on the following link: adnan-halilovic/retry-http-requests-angular: Retry Error HTTP Requests in Angular (github.com)

Source: Youtube - Retry Error HTTP Requests in Angular
Tags: AngularinterceptorsRxJS
ADVERTISEMENT
Previous Post

How to Install Node on AlmaLinux 9

Next Post

How to setup routing in ReactJS

Related Posts

VS Code Setup for Angular Developers Featured Image
Angular

Best VS Code Setup for Angular Development

October 13, 2024
My Top 5 Uncommon VS Code Extensions for 2024!
Frontend

My Top 5 Uncommon VS Code Extensions for 2024!

January 27, 2024
Mastering DevTools: Supercharge Your Workflow with Code Snippets!
Frontend

Mastering DevTools: Supercharge Your Workflow with Code Snippets!

September 24, 2023
Quokka.js and Wallaby.js Giveaway
Frontend

Quokka.js and Wallaby.js Giveaway

July 26, 2023
How to Work on Different Git Branches at the Same Time?
Git

How to Work on Different Git Branches at the Same Time?

December 4, 2022
Lazyload Standalone Components in Angular
Angular

Lazyloading Standalone Components in Angular

September 11, 2022
Next Post
How to setup routing in ReactJS

How to setup routing in ReactJS

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Latest posts

The Best Coding Font to Boost Your Productivity!
IDE

The Best Coding Font to Boost Your Productivity!

by Adnan Halilovic
October 17, 2024
0

In this article, we're diving into the world of Fira Code, a font designed specifically for coding. But is it...

Read moreDetails
VS Code Setup for Angular Developers Featured Image

Best VS Code Setup for Angular Development

October 13, 2024
My Top 5 Uncommon VS Code Extensions for 2024!

My Top 5 Uncommon VS Code Extensions for 2024!

January 27, 2024
Mastering DevTools: Supercharge Your Workflow with Code Snippets!

Mastering DevTools: Supercharge Your Workflow with Code Snippets!

September 24, 2023
Quokka.js and Wallaby.js Giveaway

Quokka.js and Wallaby.js Giveaway

July 26, 2023
Github LinkedIn Youtube Twitter Pinterest Instagram Reddit

About Me

Adnan Halilovic Blog

Adnan Halilović

Software Developer - Content Writer

I am Adnan Halilović, the man behind the website and channels that you are currently reading and viewing!

I am a software developer with over 15 years of expertise in a variety of fields.

Newsletter

Country:

Email address:


Recent from Instagram

    The Instagram Access Token is expired, Go to the Customizer > JNews : Social, Like & View > Instagram Feed Setting, to refresh it.

© 2022 Adnan Halilovic - Software development content, tips & tricks.

Click to Copy
No Result
View All Result
  • Technologies
  • Frontend
  • Backend
  • Server
  • Contact
  • About

© 2022 Adnan Halilovic - Software development content, tips & tricks.