Add retries to error message
parent
603ef97144
commit
5e3eaf254b
|
@ -1 +0,0 @@
|
||||||
reqwest-middleware/CHANGELOG.md
|
|
|
@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- `request_middleware::Error` is now a transparent error enum and doesn't add its own context anymore.
|
||||||
|
|
||||||
## [0.3.3] - 2024-07-08
|
## [0.3.3] - 2024-07-08
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
|
@ -6,10 +6,10 @@ pub type Result<T> = std::result::Result<T, Error>;
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// There was an error running some middleware
|
/// There was an error running some middleware
|
||||||
#[error("Middleware error: {0}")]
|
#[error(transparent)]
|
||||||
Middleware(#[from] anyhow::Error),
|
Middleware(#[from] anyhow::Error),
|
||||||
/// Error from the underlying reqwest client
|
/// Error from the underlying reqwest client
|
||||||
#[error("Request error: {0}")]
|
#[error(transparent)]
|
||||||
Reqwest(#[from] reqwest::Error),
|
Reqwest(#[from] reqwest::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
|
||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased]
|
## [0.6.0]
|
||||||
|
|
||||||
## [0.6.1] - 2024-08-08
|
## [0.6.1] - 2024-08-08
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- Upgraded `retry-policies` to `0.4.0`.
|
- Upgraded `retry-policies` to `0.4.0`.
|
||||||
|
- **Breaking Change** Errors are now reported as `RetryError` that adds the number of retries to the error chain if there were any. This changes the returned error types.
|
||||||
|
|
||||||
## [0.5.0] - 2024-04-10
|
## [0.5.0] - 2024-04-10
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "reqwest-retry"
|
name = "reqwest-retry"
|
||||||
version = "0.6.1"
|
version = "0.7.1"
|
||||||
authors = ["Rodrigo Gryzinski <rodrigo.gryzinski@truelayer.com>"]
|
authors = ["Rodrigo Gryzinski <rodrigo.gryzinski@truelayer.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Retry middleware for reqwest."
|
description = "Retry middleware for reqwest."
|
||||||
|
@ -18,6 +18,7 @@ futures = "0.3.0"
|
||||||
http = "1.0"
|
http = "1.0"
|
||||||
reqwest = { version = "0.12.0", default-features = false }
|
reqwest = { version = "0.12.0", default-features = false }
|
||||||
retry-policies = "0.4"
|
retry-policies = "0.4"
|
||||||
|
thiserror = "1.0.61"
|
||||||
tracing = "0.1.26"
|
tracing = "0.1.26"
|
||||||
|
|
||||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
|
|
|
@ -30,6 +30,7 @@ mod retryable;
|
||||||
mod retryable_strategy;
|
mod retryable_strategy;
|
||||||
|
|
||||||
pub use retry_policies::{policies, Jitter, RetryDecision, RetryPolicy};
|
pub use retry_policies::{policies, Jitter, RetryDecision, RetryPolicy};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
pub use middleware::RetryTransientMiddleware;
|
pub use middleware::RetryTransientMiddleware;
|
||||||
pub use retryable::Retryable;
|
pub use retryable::Retryable;
|
||||||
|
@ -37,3 +38,16 @@ pub use retryable_strategy::{
|
||||||
default_on_request_failure, default_on_request_success, DefaultRetryableStrategy,
|
default_on_request_failure, default_on_request_success, DefaultRetryableStrategy,
|
||||||
RetryableStrategy,
|
RetryableStrategy,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Custom error type to attach the number of retries to the error message.
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum RetryError {
|
||||||
|
#[error("Request failed after {retries} retries")]
|
||||||
|
WithRetries {
|
||||||
|
retries: u32,
|
||||||
|
#[source]
|
||||||
|
err: reqwest_middleware::Error,
|
||||||
|
},
|
||||||
|
#[error(transparent)]
|
||||||
|
Error(reqwest_middleware::Error),
|
||||||
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
|
|
||||||
use crate::retryable_strategy::RetryableStrategy;
|
use crate::retryable_strategy::RetryableStrategy;
|
||||||
use crate::{retryable::Retryable, retryable_strategy::DefaultRetryableStrategy};
|
use crate::{retryable::Retryable, retryable_strategy::DefaultRetryableStrategy, RetryError};
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use http::Extensions;
|
use http::Extensions;
|
||||||
use reqwest::{Request, Response};
|
use reqwest::{Request, Response};
|
||||||
|
@ -153,7 +153,7 @@ where
|
||||||
|
|
||||||
// We classify the response which will return None if not
|
// We classify the response which will return None if not
|
||||||
// errors were returned.
|
// errors were returned.
|
||||||
break match self.retryable_strategy.handle(&result) {
|
match self.retryable_strategy.handle(&result) {
|
||||||
Some(Retryable::Transient) => {
|
Some(Retryable::Transient) => {
|
||||||
// If the response failed and the error type was transient
|
// If the response failed and the error type was transient
|
||||||
// we can safely try to retry the request.
|
// we can safely try to retry the request.
|
||||||
|
@ -178,11 +178,24 @@ where
|
||||||
|
|
||||||
n_past_retries += 1;
|
n_past_retries += 1;
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(_) | None => {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Report whether we failed with or without retries.
|
||||||
|
break if n_past_retries > 0 {
|
||||||
|
result.map_err(|err| {
|
||||||
|
Error::Middleware(
|
||||||
|
RetryError::WithRetries {
|
||||||
|
retries: n_past_retries,
|
||||||
|
err,
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
)
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
result
|
result.map_err(|err| Error::Middleware(RetryError::Error(err).into()))
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(_) | None => result,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue