Skip to content

Commit

Permalink
Add retries to error message (#159)
Browse files Browse the repository at this point in the history
Co-authored-by: Ethan Brierley <ethanboxx@gmail.com>
  • Loading branch information
konstin and eopb authored Oct 16, 2024
1 parent cf06f09 commit 680741f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md

This file was deleted.

3 changes: 3 additions & 0 deletions reqwest-middleware/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [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

### Added
Expand Down
4 changes: 2 additions & 2 deletions reqwest-middleware/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
/// There was an error running some middleware
#[error("Middleware error: {0}")]
#[error(transparent)]
Middleware(#[from] anyhow::Error),
/// Error from the underlying reqwest client
#[error("Request error: {0}")]
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
}

Expand Down
3 changes: 2 additions & 1 deletion reqwest-retry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/),
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

Expand All @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- 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

Expand Down
3 changes: 2 additions & 1 deletion reqwest-retry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reqwest-retry"
version = "0.6.1"
version = "0.7.1"
authors = ["Rodrigo Gryzinski <rodrigo.gryzinski@truelayer.com>"]
edition = "2018"
description = "Retry middleware for reqwest."
Expand All @@ -22,6 +22,7 @@ futures = "0.3.0"
http = "1.0"
reqwest = { version = "0.12.0", default-features = false }
retry-policies = "0.4"
thiserror = "1.0.61"
tracing = { version = "0.1.26", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
14 changes: 14 additions & 0 deletions reqwest-retry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,24 @@ mod retryable;
mod retryable_strategy;

pub use retry_policies::{policies, Jitter, RetryDecision, RetryPolicy};
use thiserror::Error;

pub use middleware::RetryTransientMiddleware;
pub use retryable::Retryable;
pub use retryable_strategy::{
default_on_request_failure, default_on_request_success, DefaultRetryableStrategy,
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),
}
23 changes: 18 additions & 5 deletions reqwest-retry/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::time::{Duration, SystemTime};

use crate::retryable_strategy::RetryableStrategy;
use crate::{retryable::Retryable, retryable_strategy::DefaultRetryableStrategy};
use crate::{retryable::Retryable, retryable_strategy::DefaultRetryableStrategy, RetryError};
use anyhow::anyhow;
use http::Extensions;
use reqwest::{Request, Response};
Expand Down Expand Up @@ -157,7 +157,7 @@ where

// We classify the response which will return None if not
// errors were returned.
break match self.retryable_strategy.handle(&result) {
match self.retryable_strategy.handle(&result) {
Some(Retryable::Transient) => {
// If the response failed and the error type was transient
// we can safely try to retry the request.
Expand All @@ -183,11 +183,24 @@ where

n_past_retries += 1;
continue;
} else {
result
}
}
Some(_) | None => result,
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 {
result.map_err(|err| Error::Middleware(RetryError::Error(err).into()))
};
}
}
Expand Down

0 comments on commit 680741f

Please sign in to comment.