Skip to main content

oci_image/
error.rs

1use std::io;
2use std::path::PathBuf;
3
4use crate::Digest;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, thiserror::Error)]
9pub enum Error {
10    #[error("{context}: {source}")]
11    Io {
12        context: String,
13        #[source]
14        source: io::Error,
15    },
16    #[error("invalid digest {0:?} (only sha256:<64 lowercase hex> is accepted)")]
17    BadDigest(String),
18    #[error("digest mismatch: expected {expected}, got {actual}")]
19    DigestMismatch { expected: Digest, actual: Digest },
20    #[error("invalid image reference {0:?}")]
21    BadReference(String),
22    #[error("layer entry {0:?} would land outside the rootfs")]
23    Escape(PathBuf),
24    #[error("blob {0} is not in the store")]
25    MissingBlob(Digest),
26    #[error("{0}")]
27    Invalid(String),
28}
29
30/// Attach what was being done to an `io::Error`.
31pub(crate) trait Context<T> {
32    fn ctx(self, what: impl FnOnce() -> String) -> Result<T>;
33}
34
35impl<T> Context<T> for io::Result<T> {
36    fn ctx(self, what: impl FnOnce() -> String) -> Result<T> {
37        self.map_err(|source| Error::Io {
38            context: what(),
39            source,
40        })
41    }
42}