Make error paths explicit
This commit is contained in:
parent
a862400f64
commit
0c7e217135
4 changed files with 62 additions and 10 deletions
|
|
@ -20,7 +20,10 @@ pub async fn get_key(
|
|||
Path(key): Path<String>,
|
||||
) -> Result<Response, AppError> {
|
||||
let record = state.db.get(&key).await?;
|
||||
let vol = record.volumes.first().ok_or(AppError::NotFound)?;
|
||||
let vol = record
|
||||
.volumes
|
||||
.first()
|
||||
.ok_or_else(|| AppError::CorruptRecord { key: key.clone() })?;
|
||||
let location = format!("{vol}/{key}");
|
||||
Ok((StatusCode::FOUND, [(axum::http::header::LOCATION, location)]).into_response())
|
||||
}
|
||||
|
|
@ -82,7 +85,12 @@ pub async fn put_key(
|
|||
}
|
||||
|
||||
let size = Some(body.len() as i64);
|
||||
state.db.put(key, target_volumes, size).await?;
|
||||
if let Err(e) = state.db.put(key.clone(), target_volumes.clone(), size).await {
|
||||
for vol in &target_volumes {
|
||||
let _ = state.http.delete(format!("{vol}/{key}")).send().await;
|
||||
}
|
||||
return Err(e);
|
||||
}
|
||||
Ok(StatusCode::CREATED.into_response())
|
||||
}
|
||||
|
||||
|
|
@ -95,14 +103,25 @@ pub async fn delete_key(
|
|||
let mut handles = Vec::new();
|
||||
for vol in &record.volumes {
|
||||
let url = format!("{vol}/{key}");
|
||||
let client = state.http.clone();
|
||||
handles.push(tokio::spawn(async move { client.delete(&url).send().await }));
|
||||
let handle = tokio::spawn({
|
||||
let client = state.http.clone();
|
||||
async move {
|
||||
let resp = client.delete(&url).send().await.map_err(|e| {
|
||||
VolumeError::Request { url: url.clone(), source: e }
|
||||
})?;
|
||||
if !resp.status().is_success() {
|
||||
return Err(VolumeError::BadStatus { url, status: resp.status() });
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
});
|
||||
handles.push(handle);
|
||||
}
|
||||
for handle in handles {
|
||||
match handle.await {
|
||||
Ok(Err(e)) => tracing::error!("DELETE from volume failed: {e}"),
|
||||
Ok(Err(e)) => tracing::error!("{e}"),
|
||||
Err(e) => tracing::error!("volume delete task failed: {e}"),
|
||||
Ok(Ok(_)) => {}
|
||||
Ok(Ok(())) => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue