I have a basic application that downloads data from a website, but the request often returns 404 not found errors or a PrematureCloseException. My question is how can I implement my client in a way that it automatically retries if it encounters a PrematureCloseException, but allows to handle 404 later in the stream while also setting a cache when the request is successful.
Current code:
httpClient.get()
.uri(URL)
.retrieve()
.bodyToMono(ByteArrayResource.class)
//Retry until the server responds "correctly"
.retryWhen(Retry.indefinitely().filter(t -> t instanceof PrematureCloseException))
.map(ByteArrayResource::getByteArray)
.doOnNext(next -> cache.set(next)) //Set the cache only if the request is successful
//Passed to another method
.ifError(error -> logger.error("Error encountered {}", error)) //Somehow handle all exceptions (not the PrematureCloseExceptions)
.subscribe(data -> {
//Successful request
})