Skip to main content

tartarus_api/
error.rs

1use std::fmt::{self, Display, Formatter};
2use thiserror::Error;
3
4pub(crate) fn with_io_context(
5    additional: impl Display,
6    context: impl Display,
7    error: impl Into<std::io::Error>,
8) -> Error {
9    Error {
10        kind: ErrorKind::Io {
11            error: error.into(),
12            additional: additional.to_string(),
13        },
14        context: context.to_string(),
15    }
16}
17
18pub type Result<T, E = Error> = std::result::Result<T, E>;
19
20/// Represents an error that occurred when trying to configure or construct a sandbox or execute a
21/// command inside it.
22#[derive(Debug)]
23pub struct Error {
24    /// The kind of error that occurred.
25    pub kind: ErrorKind,
26
27    /// The context when the error occurred.
28    pub context: String,
29}
30
31impl std::error::Error for Error {
32    fn cause(&self) -> Option<&dyn std::error::Error> {
33        self.kind.source()
34    }
35}
36
37impl Display for Error {
38    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
39        write!(f, "encountered {} during {}", self.kind, self.context)
40    }
41}
42
43/// Represents the kind of error that occurred when trying to configure or construct a sandbox or
44/// execute a command inside it.
45#[derive(Debug, Error)]
46#[non_exhaustive]
47pub enum ErrorKind {
48    /// An I/O error occurred.
49    #[error("{error} ({additional})")]
50    Io {
51        error: std::io::Error,
52        additional: String,
53    },
54
55    /// An absolute path was provided when a relative one is needed.
56    #[error("non-relative path {0}")]
57    NonRelativePath(std::path::PathBuf),
58
59    /// The opencode config file has invalid syntax.
60    #[cfg(feature = "opencode")]
61    #[error("{0}")]
62    InvalidOpencodeConfigSyntax(jsonc_parser::errors::ParseError),
63
64    /// The opencode config file has contents that don't fit the expected schema
65    #[cfg(feature = "opencode")]
66    #[error("{0}")]
67    InvalidOpencodeConfigSchema(String),
68
69    /// The zed config file has invalid syntax.
70    #[cfg(feature = "zed")]
71    #[error("{0}")]
72    InvalidZedConfigSyntax(jsonc_parser::errors::ParseError),
73
74    /// The zed config file has contents that don't fit the expected schema
75    #[cfg(feature = "zed")]
76    #[error("{0}")]
77    InvalidZedConfigSchema(String),
78}