Repository: firecracker-microvm/firecracker
File: src/jailer/src/main.rs (Rust)

Code before:

const JAILER_VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Debug, thiserror::Error)]
pub enum JailerError {
    #[error("Failed to parse arguments: {0}")]
    ArgumentParsing(ParsingError),
    #[error("{}", format!("Failed to canonicalize path {:?}: {}", .0, .1).replace('\"', ""))]
    Canonicalize(PathBuf, io::Error),
    #[error("{}", format!("Failed to inherit cgroups configurations from file {} in path {:?}", .1, .0).replace('\"', ""))]
    CgroupInheritFromParent(PathBuf, String),
    #[error("{1} configurations not found in {0}")]
    CgroupLineNotFound(String, String),
    #[error("Cgroup invalid file: {0}")]
    CgroupInvalidFile(String),
    #[error("Invalid format for cgroups: {0}")]
    CgroupFormat(String),
    #[error("Hierarchy not found: {0}")]
    CgroupHierarchyMissing(String),
    #[error("Controller {0} is unavailable")]
    CgroupControllerUnavailable(String),
    #[error("{0} is an invalid cgroup version specifier")]
    CgroupInvalidVersion(String),
    #[error("Parent cgroup path is invalid. Path should not be absolute or contain '..' or '.'")]
    CgroupInvalidParentPath(),
    #[error("Failed to move process to cgroup ({0}) due to no internal process constraint: {1}")]
    CgroupMove(PathBuf, io::Error),
    #[error("Failed to change owner for {0}: {1}")]
    ChangeFileOwner(PathBuf, io::Error),
    #[error("Failed to chdir into chroot directory: {0}")]
    ChdirNewRoot(io::Error),
    #[error("Failed to change permissions on {0}: {1}")]
    Chmod(PathBuf, io::Error),
    #[error("Failed cloning into a new child process: {0}")]
    Clone(io::Error),
    #[error("Failed to close netns fd: {0}")]
    CloseNetNsFd(io::Error),
    #[error("Failed to close /dev/null fd: {0}")]
    CloseDevNullFd(io::Error),
    #[error("Failed to call close range syscall: {0}")]
    CloseRange(io::Error),
    #[error("{}", format!("Failed to copy {:?} to {:?}: {}", .0, .1, .2).replace('\"', ""))]
    Copy(PathBuf, PathBuf, io::Error),
    #[error("{}", format!("Failed to create directory {:?}: {}", .0, .1).replace('\"', ""))]
    CreateDir(PathBuf, io::Error),
    #[error("Encountered interior \\0 while parsing a string")]
    CStringParsing(NulError),
    #[error("Failed to daemonize: {0}")]
    Daemonize(io::Error),
    #[error("Failed to open directory {0}: {1}")]
    DirOpen(String, String),

Review comment (bug):
can it fail for other reasons? or does any failure (errno) mean there's an internal process?

We could also add a hint: "Maybe you meant to create a child cgroup, which requires passing any `--cgroup` option?".

Code after:

const JAILER_VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Debug, thiserror::Error)]
pub enum JailerError {
    #[error("Failed to parse arguments: {0}")]
    ArgumentParsing(ParsingError),
    #[error("{}", format!("Failed to canonicalize path {:?}: {}", .0, .1).replace('\"', ""))]
    Canonicalize(PathBuf, io::Error),
    #[error("{}", format!("Failed to inherit cgroups configurations from file {} in path {:?}", .1, .0).replace('\"', ""))]
    CgroupInheritFromParent(PathBuf, String),
    #[error("{1} configurations not found in {0}")]
    CgroupLineNotFound(String, String),
    #[error("Cgroup invalid file: {0}")]
    CgroupInvalidFile(String),
    #[error("Invalid format for cgroups: {0}")]
    CgroupFormat(String),
    #[error("Hierarchy not found: {0}")]
    CgroupHierarchyMissing(String),
    #[error("Controller {0} is unavailable")]
    CgroupControllerUnavailable(String),
    #[error("{0} is an invalid cgroup version specifier")]
    CgroupInvalidVersion(String),
    #[error("Parent cgroup path is invalid. Path should not be absolute or contain '..' or '.'")]
    CgroupInvalidParentPath(),
    #[error(
        "Failed to move process to cgroup ({0}): {1}.\nHint: If you intended to create a child \
         cgroup under {0}, pass any --cgroup parameters."
    )]
    CgroupMove(PathBuf, io::Error),
    #[error("Failed to change owner for {0}: {1}")]
    ChangeFileOwner(PathBuf, io::Error),
    #[error("Failed to chdir into chroot directory: {0}")]
    ChdirNewRoot(io::Error),
    #[error("Failed to change permissions on {0}: {1}")]
    Chmod(PathBuf, io::Error),
    #[error("Failed cloning into a new child process: {0}")]
    Clone(io::Error),
    #[error("Failed to close netns fd: {0}")]
    CloseNetNsFd(io::Error),
    #[error("Failed to close /dev/null fd: {0}")]
    CloseDevNullFd(io::Error),
    #[error("Failed to call close range syscall: {0}")]
    CloseRange(io::Error),
    #[error("{}", format!("Failed to copy {:?} to {:?}: {}", .0, .1, .2).replace('\"', ""))]
    Copy(PathBuf, PathBuf, io::Error),
    #[error("{}", format!("Failed to create directory {:?}: {}", .0, .1).replace('\"', ""))]
    CreateDir(PathBuf, io::Error),
    #[error("Encountered interior \\0 while parsing a string")]
    CStringParsing(NulError),
    #[error("Failed to daemonize: {0}")]