Skip to content

Commit

Permalink
aw-datastore: Add bucket_id to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
johan-bjareholt committed Aug 6, 2020
1 parent 7a19f34 commit 5bdd6f3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
14 changes: 9 additions & 5 deletions aw-datastore/src/datastore.rs
Expand Up @@ -382,7 +382,7 @@ impl DatastoreInstance {
Err(err) => match err {
rusqlite::Error::SqliteFailure { 0: sqlerr, 1: _ } => match sqlerr.code {
rusqlite::ErrorCode::ConstraintViolation => {
Err(DatastoreError::BucketAlreadyExists)
Err(DatastoreError::BucketAlreadyExists(bucket.id.to_string()))
}
_ => Err(DatastoreError::InternalError(format!(
"Failed to execute create_bucket SQL statement: {}",
Expand Down Expand Up @@ -417,7 +417,7 @@ impl DatastoreInstance {
Err(err) => match err {
rusqlite::Error::SqliteFailure { 0: sqlerr, 1: _ } => match sqlerr.code {
rusqlite::ErrorCode::ConstraintViolation => {
Err(DatastoreError::BucketAlreadyExists)
Err(DatastoreError::BucketAlreadyExists(bucket_id.to_string()))
}
_ => Err(DatastoreError::InternalError(err.to_string())),
},
Expand All @@ -430,7 +430,7 @@ impl DatastoreInstance {
let cached_bucket = self.buckets_cache.get(bucket_id);
match cached_bucket {
Some(bucket) => Ok(bucket.clone()),
None => Err(DatastoreError::NoSuchBucket),
None => Err(DatastoreError::NoSuchBucket(bucket_id.to_string())),
}
}

Expand Down Expand Up @@ -881,7 +881,9 @@ impl DatastoreInstance {
}) {
Ok(result) => Ok(result),
Err(err) => match err {
rusqlite::Error::QueryReturnedNoRows => Err(DatastoreError::NoSuchKey),
rusqlite::Error::QueryReturnedNoRows => {
Err(DatastoreError::NoSuchKey(key.to_string()))
}
_ => Err(DatastoreError::InternalError(format!(
"Get value query failed for key {}",
key
Expand Down Expand Up @@ -918,7 +920,9 @@ impl DatastoreInstance {
Ok(output)
}
Err(err) => match err {
rusqlite::Error::QueryReturnedNoRows => Err(DatastoreError::NoSuchKey),
rusqlite::Error::QueryReturnedNoRows => {
Err(DatastoreError::NoSuchKey(pattern.to_string()))
}
_ => Err(DatastoreError::InternalError(format!(
"Failed to get key_value rows starting with pattern {}",
pattern
Expand Down
6 changes: 3 additions & 3 deletions aw-datastore/src/lib.rs
Expand Up @@ -30,9 +30,9 @@ pub enum DatastoreMethod {
/* TODO: Implement this as a proper error */
#[derive(Debug, Clone)]
pub enum DatastoreError {
NoSuchBucket,
BucketAlreadyExists,
NoSuchKey,
NoSuchBucket(String),
BucketAlreadyExists(String),
NoSuchKey(String),
MpscError,
InternalError(String),
// Errors specific to when migrate is disabled
Expand Down
19 changes: 11 additions & 8 deletions aw-server/src/endpoints/util.rs
Expand Up @@ -23,9 +23,11 @@ impl HttpErrorJson {

impl<'r> Responder<'r> for HttpErrorJson {
fn respond_to(self, _: &Request) -> response::Result<'r> {
// TODO: Fix unwrap
let body = serde_json::to_string(&self).unwrap();
Response::build()
.status(self.status)
.sized_body(Cursor::new(format!("{{\"message\":\"{}\"}}", self.message)))
.sized_body(Cursor::new(body))
.header(ContentType::new("application", "json"))
.ok()
}
Expand All @@ -36,16 +38,17 @@ use aw_datastore::DatastoreError;
impl Into<HttpErrorJson> for DatastoreError {
fn into(self) -> HttpErrorJson {
match self {
DatastoreError::NoSuchBucket => HttpErrorJson::new(
DatastoreError::NoSuchBucket(bucket_id) => HttpErrorJson::new(
Status::NotFound,
"The requested bucket does not exist".to_string(),
format!("The requested bucket '{}' does not exist", bucket_id),
),
DatastoreError::BucketAlreadyExists => {
HttpErrorJson::new(Status::NotModified, "Bucket already exists".to_string())
}
DatastoreError::NoSuchKey => HttpErrorJson::new(
DatastoreError::BucketAlreadyExists(bucket_id) => HttpErrorJson::new(
Status::NotModified,
format!("Bucket '{}' already exists", bucket_id),
),
DatastoreError::NoSuchKey(key) => HttpErrorJson::new(
Status::NotFound,
"The requested key does not exist".to_string(),
format!("The requested key(s) '{}' do not exist", key),
),
DatastoreError::MpscError => HttpErrorJson::new(
Status::InternalServerError,
Expand Down
4 changes: 2 additions & 2 deletions aw-server/tests/api.rs
Expand Up @@ -89,7 +89,7 @@ mod api_tests {
assert_eq!(res.status(), rocket::http::Status::NotModified);
assert_eq!(
res.body_string().unwrap(),
"{\"message\":\"Bucket already exists\"}"
r#"{"message":"Bucket 'id' already exists"}"#
);

// Get list of buckets (1 bucket)
Expand Down Expand Up @@ -316,7 +316,7 @@ mod api_tests {
assert_eq!(res.status(), rocket::http::Status::InternalServerError);
assert_eq!(
res.body_string().unwrap(),
"{\"message\":\"Failed to import bucket: BucketAlreadyExists\"}"
r#"{"message":"Failed to import bucket: BucketAlreadyExists(\"id1\")"}"#
);

// Export single created bucket
Expand Down
4 changes: 2 additions & 2 deletions aw-sync/src/sync.rs
Expand Up @@ -165,7 +165,7 @@ fn setup_test(sync_directory: &Path) -> std::io::Result<Vec<Datastore>> {
match ds.create_bucket(&bucket) {
Ok(()) => (),
Err(e) => match e {
DatastoreError::BucketAlreadyExists => {
DatastoreError::BucketAlreadyExists(_) => {
debug!("bucket already exists, skipping");
}
e => panic!("woops! {:?}", e),
Expand Down Expand Up @@ -205,7 +205,7 @@ fn get_or_create_sync_bucket(bucket_from: &Bucket, ds_to: &dyn AccessMethod) ->

match ds_to.get_bucket(new_id.as_str()) {
Ok(bucket) => bucket,
Err(DatastoreError::NoSuchBucket) => {
Err(DatastoreError::NoSuchBucket(_)) => {
let mut bucket_new = bucket_from.clone();
bucket_new.id = new_id.clone();
// TODO: Replace sync origin with hostname/GUID and discuss how we will treat the data
Expand Down
2 changes: 1 addition & 1 deletion aw-sync/tests/sync.rs
Expand Up @@ -39,7 +39,7 @@ mod sync_tests {
match ds.create_bucket(&bucket) {
Ok(()) => (),
Err(e) => match e {
DatastoreError::BucketAlreadyExists => {
DatastoreError::BucketAlreadyExists(_) => {
debug!("bucket already exists, skipping");
}
e => panic!("woops! {:?}", e),
Expand Down

0 comments on commit 5bdd6f3

Please sign in to comment.