Created
May 23, 2024 04:04
-
-
Save jl91/85f78d43ab979f74f098b82e8904d7e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.petService.getPetById(1) | |
.pipe( | |
retryWhen(errors => errors.pipe( | |
mergeMap((error: HttpErrorResponse) => { | |
if (error.status === 404) { | |
return this.authService.refreshToken().pipe( | |
tap(newToken => { | |
if (newToken) { | |
this.token = newToken; | |
} | |
}), | |
catchError(refreshError => { | |
// Se a renovação falhar, propaga o erro original | |
return throwError(error); | |
}) | |
); | |
} | |
// Para qualquer outro erro, propaga o erro | |
return throwError(error); | |
}), | |
delay(1000), // Delay opcional entre tentativas | |
scan((retryCount, error) => { | |
if (retryCount >= 3) { | |
throw error; | |
} else { | |
return retryCount + 1; | |
} | |
}, 0), | |
)), | |
) | |
.subscribe((data) => { | |
console.log(data); | |
}, | |
(error) => { | |
console.error(error); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment