mirror of
https://github.com/deuxfleurs-org/garage.git
synced 2026-08-10 22:36:53 +00:00
Some movement of helper code and refactoring of error handling
This commit is contained in:
@@ -21,6 +21,7 @@ garage_model_050 = { package = "garage_model", version = "0.5.1" }
|
||||
|
||||
async-trait = "0.1.7"
|
||||
arc-swap = "1.0"
|
||||
err-derive = "0.3"
|
||||
hex = "0.4"
|
||||
log = "0.4"
|
||||
rand = "0.8"
|
||||
|
||||
+3
-5
@@ -594,10 +594,8 @@ impl BlockManager {
|
||||
need_nodes.push(*node);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(Error::Message(
|
||||
"Unexpected response to NeedBlockQuery RPC".to_string(),
|
||||
));
|
||||
m => {
|
||||
return Err(Error::unexpected_rpc_message(m));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -730,7 +728,7 @@ impl EndpointHandler<BlockRpc> for BlockManager {
|
||||
BlockRpc::PutBlock { hash, data } => self.write_block(hash, data).await,
|
||||
BlockRpc::GetBlock(h) => self.read_block(h).await,
|
||||
BlockRpc::NeedBlockQuery(h) => self.need_block(h).await.map(BlockRpc::NeedBlockReply),
|
||||
_ => Err(Error::BadRpc("Unexpected RPC message".to_string())),
|
||||
m => Err(Error::unexpected_rpc_message(m)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -15,8 +15,8 @@ use garage_table::*;
|
||||
use crate::block::*;
|
||||
use crate::block_ref_table::*;
|
||||
use crate::bucket_alias_table::*;
|
||||
use crate::bucket_helper::*;
|
||||
use crate::bucket_table::*;
|
||||
use crate::helper;
|
||||
use crate::key_table::*;
|
||||
use crate::object_table::*;
|
||||
use crate::version_table::*;
|
||||
@@ -162,7 +162,7 @@ impl Garage {
|
||||
self.block_manager.garage.swap(None);
|
||||
}
|
||||
|
||||
pub fn bucket_helper(&self) -> BucketHelper {
|
||||
BucketHelper(self)
|
||||
pub fn bucket_helper(&self) -> helper::bucket::BucketHelper {
|
||||
helper::bucket::BucketHelper(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use garage_util::data::*;
|
||||
use garage_util::error::*;
|
||||
|
||||
use garage_table::util::EmptyKey;
|
||||
use garage_util::data::*;
|
||||
|
||||
use crate::bucket_table::Bucket;
|
||||
use crate::garage::Garage;
|
||||
use crate::helper::error::*;
|
||||
|
||||
pub struct BucketHelper<'a>(pub(crate) &'a Garage);
|
||||
|
||||
@@ -52,12 +51,6 @@ impl<'a> BucketHelper<'a> {
|
||||
.get(&bucket_id, &EmptyKey)
|
||||
.await?
|
||||
.filter(|b| !b.is_deleted())
|
||||
.map(Ok)
|
||||
.unwrap_or_else(|| {
|
||||
Err(Error::BadRpc(format!(
|
||||
"Bucket {:?} does not exist",
|
||||
bucket_id
|
||||
)))
|
||||
})
|
||||
.ok_or_bad_request(format!("Bucket {:?} does not exist", bucket_id))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
use err_derive::Error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use garage_util::error::Error as GarageError;
|
||||
|
||||
#[derive(Debug, Error, Serialize, Deserialize)]
|
||||
pub enum Error {
|
||||
#[error(display = "Internal error: {}", _0)]
|
||||
Internal(#[error(source)] GarageError),
|
||||
|
||||
#[error(display = "Bad request: {}", _0)]
|
||||
BadRequest(String),
|
||||
}
|
||||
|
||||
impl From<netapp::error::Error> for Error {
|
||||
fn from(e: netapp::error::Error) -> Self {
|
||||
Error::Internal(GarageError::Netapp(e))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait OkOrBadRequest {
|
||||
type S;
|
||||
fn ok_or_bad_request<M: AsRef<str>>(self, reason: M) -> Result<Self::S, Error>;
|
||||
}
|
||||
|
||||
impl<T, E> OkOrBadRequest for Result<T, E>
|
||||
where
|
||||
E: std::fmt::Display,
|
||||
{
|
||||
type S = T;
|
||||
fn ok_or_bad_request<M: AsRef<str>>(self, reason: M) -> Result<T, Error> {
|
||||
match self {
|
||||
Ok(x) => Ok(x),
|
||||
Err(e) => Err(Error::BadRequest(format!(
|
||||
"{}: {}",
|
||||
reason.as_ref(),
|
||||
e.to_string()
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> OkOrBadRequest for Option<T> {
|
||||
type S = T;
|
||||
fn ok_or_bad_request<M: AsRef<str>>(self, reason: M) -> Result<T, Error> {
|
||||
match self {
|
||||
Some(x) => Ok(x),
|
||||
None => Err(Error::BadRequest(reason.as_ref().to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod bucket;
|
||||
pub mod error;
|
||||
+1
-1
@@ -12,6 +12,6 @@ pub mod version_table;
|
||||
|
||||
pub mod block;
|
||||
|
||||
pub mod bucket_helper;
|
||||
pub mod garage;
|
||||
pub mod helper;
|
||||
pub mod migrate;
|
||||
|
||||
Reference in New Issue
Block a user