rust: podcast downloads carry their errors typed, and record the copy in one transaction
PodcastError::Http(String) and Blob(String), and the seven
map_err(to_string)s that fed them, are #[from] reqwest::Error and
BlobStoreError; the release path's EvictionError gets its own Release
variant instead of being reported as 'failed to store'. The status check is
reqwest's error_for_status. save_episode_audio writes the PrimaryContent
artifact and the mark_present in one transaction instead of two (audit
P-9). A failed download's message now reads as reqwest words it
('HTTP status client error (404 Not Found) for url (...)') rather than
'404 Not Found for ...'.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3ade17ce73
commit
4eed02cf37
2 files changed
+25
-39
No files matched your search
@@ -16,12 +16,14 @@ use crate::documents::{
|
||||
Artifact, ArtifactKind, ArtifactValue, BlobRef, Document, DocumentID, DocumentType,
|
||||
ListenState, Property, PropertyKind, PropertyValue, Provenance, Timestamp,
|
||||
};
|
||||
use crate::eviction::EvictionError;
|
||||
use crate::node::NodePubkey;
|
||||
use crate::stores::Store;
|
||||
use crate::stores::artifact::{ArtifactStore, ArtifactStoreError};
|
||||
use crate::stores::blob_store::StoreProgress;
|
||||
use crate::stores::blob_store::{BlobStoreError, StoreProgress};
|
||||
use crate::stores::document::DocumentStoreError;
|
||||
use crate::stores::node_document_status::NodeDocumentStatusError;
|
||||
use crate::vault::DEFAULT_VAULT_ID;
|
||||
|
||||
/// Failures from this module's store operations.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
@@ -43,11 +45,15 @@ pub enum PodcastError {
|
||||
#[error("podcast episode has no audio to download")]
|
||||
NoEnclosure,
|
||||
#[error("failed to download podcast audio: {0}")]
|
||||
Http(String),
|
||||
Http(#[from] reqwest::Error),
|
||||
#[error("failed to write podcast audio: {0}")]
|
||||
Download(#[source] std::io::Error),
|
||||
#[error("failed to store podcast audio: {0}")]
|
||||
Blob(String),
|
||||
Blob(#[from] BlobStoreError),
|
||||
/// Recording that this node dropped its copy, for peers that count it as
|
||||
/// a holder.
|
||||
#[error(transparent)]
|
||||
Release(#[from] EvictionError),
|
||||
/// Only reachable from `Store::batch`'s own connection-open/commit
|
||||
/// machinery, not from any store method — needed so a multi-step `batch`
|
||||
/// closure here can use `PodcastError` as its error type directly.
|
||||
@@ -642,27 +648,15 @@ pub fn save_episode_audio(
|
||||
// same bytes, and a bar that reaches the end and starts again is worse
|
||||
// than one that stops short — the network is the wait here, and the
|
||||
// sealing that follows it is local disk.
|
||||
let stored = store
|
||||
.blob_store
|
||||
.store_file(staged.path(), crate::vault::DEFAULT_VAULT_ID, None)
|
||||
.map_err(|e| PodcastError::Blob(e.to_string()))?;
|
||||
|
||||
store.artifact_store.batch(|tx| {
|
||||
store.artifact_store.upsert_artifact(
|
||||
tx,
|
||||
&Artifact::new(
|
||||
document_id.clone(),
|
||||
ArtifactValue::PrimaryContent(BlobRef::new(
|
||||
stored.stored_hash,
|
||||
stored.plaintext_hash,
|
||||
crate::vault::DEFAULT_VAULT_ID,
|
||||
plaintext_len,
|
||||
)),
|
||||
node,
|
||||
),
|
||||
)
|
||||
let stored = store.blob_store.store_file(staged.path(), DEFAULT_VAULT_ID, None)?;
|
||||
let blob_ref =
|
||||
BlobRef::new(stored.stored_hash, stored.plaintext_hash, DEFAULT_VAULT_ID, plaintext_len);
|
||||
let artifact =
|
||||
Artifact::new(document_id.clone(), ArtifactValue::PrimaryContent(blob_ref), node);
|
||||
store.batch(|tx| {
|
||||
store.artifact_store.upsert_artifact(tx, &artifact)?;
|
||||
Ok::<_, PodcastError>(store.node_doc_status_store.mark_present(tx, &document_id, &node)?)
|
||||
})?;
|
||||
store.batch(|tx| store.node_doc_status_store.mark_present(tx, &document_id, &node))?;
|
||||
Ok(document_id)
|
||||
}
|
||||
|
||||
@@ -675,12 +669,7 @@ fn download_to(
|
||||
path: &std::path::Path,
|
||||
progress: Option<&dyn StoreProgress>,
|
||||
) -> Result<u64, PodcastError> {
|
||||
let client =
|
||||
crate::http::build_blocking_client().map_err(|e| PodcastError::Http(e.to_string()))?;
|
||||
let mut response = client.get(url).send().map_err(|e| PodcastError::Http(e.to_string()))?;
|
||||
if !response.status().is_success() {
|
||||
return Err(PodcastError::Http(format!("{} for {url}", response.status())));
|
||||
}
|
||||
let mut response = crate::http::build_blocking_client()?.get(url).send()?.error_for_status()?;
|
||||
|
||||
let mut out =
|
||||
std::io::BufWriter::new(std::fs::File::create(path).map_err(PodcastError::Download)?);
|
||||
@@ -729,19 +718,14 @@ pub fn release_episode_audio(store: &Store, episode_id: &DocumentID) -> Result<b
|
||||
else {
|
||||
return Ok(false);
|
||||
};
|
||||
if !store
|
||||
.blob_store
|
||||
.delete(&blob_ref.blob_hash)
|
||||
.map_err(|e| PodcastError::Blob(e.to_string()))?
|
||||
{
|
||||
if !store.blob_store.delete(&blob_ref.blob_hash)? {
|
||||
return Ok(false);
|
||||
}
|
||||
crate::eviction::record_local_release(
|
||||
store,
|
||||
std::slice::from_ref(&blob_ref.blob_hash),
|
||||
&std::collections::HashSet::from([episode_id.clone()]),
|
||||
)
|
||||
.map_err(|e| PodcastError::Blob(e.to_string()))?;
|
||||
)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
|
||||
@@ -411,9 +411,11 @@ impl From<podcast::PodcastError> for SyncError {
|
||||
// The publisher's server, not a peer — but the shape a caller has
|
||||
// to handle is the same one every unreachable source produces.
|
||||
E::Http(..) => SyncError::Offline { msg },
|
||||
E::Download(..) | E::Blob(..) | E::NodeDocumentStatus(..) | E::Sqlite(..) => {
|
||||
SyncError::Internal { msg }
|
||||
}
|
||||
E::Download(..)
|
||||
| E::Blob(..)
|
||||
| E::Release(..)
|
||||
| E::NodeDocumentStatus(..)
|
||||
| E::Sqlite(..) => SyncError::Internal { msg },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user