cacache_sync/
errors.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6#[error("{source}\n\n  {}", context.join("\n  "))]
7pub struct InternalError {
8    source: Box<dyn std::error::Error + Send + Sync>,
9    context: Vec<String>,
10}
11
12pub trait Internal<T> {
13    fn to_internal(self) -> InternalResult<T>;
14    fn with_context<F: FnOnce() -> String>(self, f: F) -> InternalResult<T>;
15}
16
17impl<T, E: 'static + std::error::Error + Send + Sync> Internal<T> for std::result::Result<T, E> {
18    fn to_internal(self) -> InternalResult<T> {
19        self.map_err(|e| InternalError {
20            source: Box::new(e),
21            context: Vec::new(),
22        })
23    }
24
25    fn with_context<F: FnOnce() -> String>(self, f: F) -> InternalResult<T> {
26        self.map_err(|e| InternalError {
27            source: Box::new(e),
28            context: vec![f()],
29        })
30    }
31}
32
33/// Error type returned by all API calls.
34#[derive(Error, Debug)]
35pub enum Error {
36    /// Returned when an index entry could not be found during
37    /// lookup.
38    #[error("Entry not found for key {1:?} in cache {0:?}")]
39    EntryNotFound(PathBuf, String),
40
41    /// Returned when a size check has failed.
42    #[error("Size check failed.\n\tWanted: {0}\n\tActual: {1}")]
43    SizeError(usize, usize),
44
45    /// Returned when an integrity check has failed.
46    #[error(transparent)]
47    IntegrityError {
48        #[from]
49        /// The underlying error
50        source: ssri::Error,
51    },
52
53    /// Returned if an internal (e.g. io) operation has failed.
54    #[error(transparent)]
55    InternalError {
56        #[from]
57        /// The underlying error
58        source: InternalError,
59    },
60}
61
62/// The result type returned by calls to this library
63pub type Result<T> = std::result::Result<T, Error>;
64
65pub type InternalResult<T> = std::result::Result<T, InternalError>;