'Duplicate block in catch block

Issue details

Duplicate code in catch block is present not just in different components but also in many functions inside a component.

Duplicate Code

if(errors.status == 401) {
    localStorage.removeItem("api_token");
    thisScope.router.navigateByUrl('', { skipLocationChange: false }).then(() => {
        thisScope.router.navigate(['/']);
        location.reload();
    }); 
}

Complete Code

GetRoles() {
    this.http.post("url", 
    {

    }, {headers: headers}).subscribe((success: any) => {
      
    },(errors: any) => {
        if(errors.status == 401) {
            localStorage.removeItem("api_token");
            thisScope.router.navigateByUrl('', { skipLocationChange: false }).then(() => {
                thisScope.router.navigate(['/']);
                location.reload();
            }); 
        }
    });
}

Can you please suggest how to fix this redundancy?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source