Skip to content

Commit

Permalink
refactor(backend::rest): factorize RusticError mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
nardoor committed Nov 17, 2024
1 parent 6bae412 commit 135829b
Showing 1 changed file with 14 additions and 42 deletions.
56 changes: 14 additions & 42 deletions crates/backend/src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ impl backon_extension::NotifyWhenRetry for reqwest::Error {
}
}

fn construct_backoff_error(err: reqwest::Error) -> Box<RusticError> {
RusticError::with_source(
ErrorKind::Backend,
"Backoff failed, please check the logs for more information.",
err,
)
}

/// A backend implementation that uses REST to access the backend.
#[derive(Clone, Debug)]
pub struct RestBackend {
Expand Down Expand Up @@ -333,13 +341,7 @@ impl ReadBackend for RestBackend {
})
.collect())
})
.map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Backoff failed, please check the logs for more information.",
err,
)
})
.map_err(construct_backoff_error)
}

/// Returns the content of a file.
Expand Down Expand Up @@ -369,13 +371,7 @@ impl ReadBackend for RestBackend {
.error_for_status()?
.bytes()?)
})
.map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Backoff failed, please check the logs for more information.",
err,
)
})
.map_err(construct_backoff_error)
}

/// Returns a part of the content of a file.
Expand Down Expand Up @@ -420,13 +416,7 @@ impl ReadBackend for RestBackend {
.error_for_status()?
.bytes()?)
})
.map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Backoff failed, please check the logs for more information.",
err,
)
})
.map_err(construct_backoff_error)
}
}

Expand Down Expand Up @@ -465,13 +455,7 @@ impl WriteBackend for RestBackend {
_ = self.client.post(url.clone()).send()?.error_for_status()?;
Ok(())
})
.map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Backoff failed, please check the logs for more information.",
err,
)
})
.map_err(construct_backoff_error)
}

/// Writes bytes to the given file.
Expand Down Expand Up @@ -512,13 +496,7 @@ impl WriteBackend for RestBackend {
.error_for_status()?;
Ok(())
})
.map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Backoff failed, please check the logs for more information.",
err,
)
})
.map_err(construct_backoff_error)
}

/// Removes the given file.
Expand All @@ -543,12 +521,6 @@ impl WriteBackend for RestBackend {
_ = self.client.delete(url.clone()).send()?.error_for_status()?;
Ok(())
})
.map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Backoff failed, please check the logs for more information.",
err,
)
})
.map_err(construct_backoff_error)
}
}

0 comments on commit 135829b

Please sign in to comment.