azure_core/policies/retry_policies/
no_retry.rs

1use crate::error::{Error, ErrorKind, HttpError};
2use crate::policies::{Policy, PolicyResult, Request};
3use crate::Context;
4use std::sync::Arc;
5
6/// Retry policy that does not retry.
7///
8/// Use this policy as a stub to disable retry policies altogether.
9#[derive(Default, Debug, Clone, PartialEq, Eq)]
10pub struct NoRetryPolicy {
11    _priv: std::marker::PhantomData<u32>,
12}
13
14#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
15#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
16impl Policy for NoRetryPolicy {
17    async fn send(
18        &self,
19        ctx: &Context,
20        request: &mut Request,
21        next: &[Arc<dyn Policy>],
22    ) -> PolicyResult {
23        // just call the following policies and bubble up the error
24        let response = next[0].send(ctx, request, &next[1..]).await?;
25
26        if response.status().is_success() {
27            Ok(response)
28        } else {
29            let status = response.status();
30            let http_error = HttpError::new(response).await;
31
32            let error_kind = ErrorKind::http_response(
33                status,
34                http_error.error_code().map(std::borrow::ToOwned::to_owned),
35            );
36            let error = Error::full(
37                error_kind,
38                http_error,
39                format!("server returned error status which will not be retried: {status}"),
40            );
41            return Err(error);
42        }
43    }
44}