peace_rt_model_core/error/
native_error.rs

1use std::{ffi::OsString, path::PathBuf, sync::Mutex};
2
3use peace_profile_model::ProfileInvalidFmt;
4use peace_resource_rt::paths::WorkspaceDir;
5
6/// Peace runtime errors.
7#[cfg_attr(feature = "error_reporting", derive(miette::Diagnostic))]
8#[derive(Debug, thiserror::Error)]
9pub enum NativeError {
10    /// Failed to present data.
11    #[error("Failed to present data.")]
12    #[cfg_attr(
13        feature = "error_reporting",
14        diagnostic(code(peace_rt_model_native::cli_output_present))
15    )]
16    CliOutputPresent(#[source] std::io::Error),
17
18    #[error("Failed to set current dir to workspace directory: `{}`", workspace_dir.display())]
19    #[cfg_attr(
20        feature = "error_reporting",
21        diagnostic(code(peace_rt_model_native::current_dir_set))
22    )]
23    CurrentDirSet {
24        /// The workspace directory.
25        workspace_dir: WorkspaceDir,
26        /// Underlying IO error
27        #[source]
28        error: std::io::Error,
29    },
30
31    /// Failed to create file for writing.
32    #[error("Failed to create file for writing: `{path}`")]
33    #[cfg_attr(
34        feature = "error_reporting",
35        diagnostic(code(peace_rt_model_native::file_create))
36    )]
37    FileCreate {
38        /// Path to the file.
39        path: PathBuf,
40        /// Underlying IO error.
41        #[source]
42        error: std::io::Error,
43    },
44
45    /// Failed to open file for reading.
46    #[error("Failed to open file for reading: `{path}`")]
47    #[cfg_attr(
48        feature = "error_reporting",
49        diagnostic(code(peace_rt_model_native::file_open))
50    )]
51    FileOpen {
52        /// Path to the file.
53        path: PathBuf,
54        /// Underlying IO error.
55        #[source]
56        error: std::io::Error,
57    },
58
59    /// Failed to read from file.
60    #[error("Failed to read from file: `{path}`")]
61    #[cfg_attr(
62        feature = "error_reporting",
63        diagnostic(code(peace_rt_model_native::file_read))
64    )]
65    FileRead {
66        /// Path to the file.
67        path: PathBuf,
68        /// Underlying IO error.
69        #[source]
70        error: std::io::Error,
71    },
72
73    /// Failed to write to file.
74    #[error("Failed to write to file: `{path}`")]
75    #[cfg_attr(
76        feature = "error_reporting",
77        diagnostic(code(peace_rt_model_native::file_write))
78    )]
79    FileWrite {
80        /// Path to the file.
81        path: PathBuf,
82        /// Underlying IO error.
83        #[source]
84        error: std::io::Error,
85    },
86
87    /// Failed to list entries in `PeaceAppDir`.
88    #[error("Failed to list entries in `PeaceAppDir`: {}", peace_app_dir.display())]
89    PeaceAppDirRead {
90        /// Path to the `PeaceAppDir`.
91        peace_app_dir: PathBuf,
92        /// Underlying IO error.
93        #[source]
94        error: std::io::Error,
95    },
96
97    /// Failed to read entry in `PeaceAppDir`.
98    #[error("Failed to read entry in `PeaceAppDir`: {}", peace_app_dir.display())]
99    PeaceAppDirEntryRead {
100        /// Path to the `PeaceAppDir`.
101        peace_app_dir: PathBuf,
102        /// Underlying IO error.
103        #[source]
104        error: std::io::Error,
105    },
106
107    /// Failed to read entry file type in `PeaceAppDir`.
108    #[error("Failed to read entry file type in `PeaceAppDir`: {}", path.display())]
109    PeaceAppDirEntryFileTypeRead {
110        /// Path to the entry within `PeaceAppDir`.
111        path: PathBuf,
112        /// Underlying IO error.
113        #[source]
114        error: std::io::Error,
115    },
116
117    /// Profile directory name is not a valid profile name.
118    #[error("Profile directory name is not a valid profile name: {}, path: {}", dir_name, path.display())]
119    #[cfg_attr(
120        feature = "error_reporting",
121        diagnostic(code(peace_rt_model_native::profile_dir_invalid_name))
122    )]
123    ProfileDirInvalidName {
124        /// Name of the directory attempted to be parsed as a `Profile`.
125        dir_name: String,
126        /// Path to the profile directory.
127        path: PathBuf,
128        /// Underlying error,
129        error: ProfileInvalidFmt<'static>,
130    },
131
132    /// Failed to write to stdout.
133    #[error("Failed to write to stdout.")]
134    #[cfg_attr(
135        feature = "error_reporting",
136        diagnostic(code(peace_rt_model_native::stdout_write))
137    )]
138    StdoutWrite(#[source] std::io::Error),
139
140    /// Storage synchronous thread failed to be joined.
141    ///
142    /// This variant is used for thread spawning errors for both reads and
143    /// writes.
144    #[error("Storage synchronous thread failed to be joined.")]
145    #[cfg_attr(
146        feature = "error_reporting",
147        diagnostic(code(peace_rt_model_native::storage_sync_thread_spawn))
148    )]
149    StorageSyncThreadSpawn(#[source] std::io::Error),
150
151    /// Storage synchronous thread failed to be joined.
152    ///
153    /// This variant is used for thread spawning errors for both reads and
154    /// writes.
155    ///
156    /// Note: The underlying thread join error does not implement
157    /// `std::error::Error`. See
158    /// <https://doc.rust-lang.org/std/thread/type.Result.html>.
159    ///
160    /// The `Mutex` is needed to allow `Error` to be `Sync`.
161    #[error("Storage synchronous thread failed to be joined.")]
162    #[cfg_attr(
163        feature = "error_reporting",
164        diagnostic(code(peace_rt_model_native::storage_sync_thread_join))
165    )]
166    StorageSyncThreadJoin(Mutex<Box<dyn std::any::Any + Send + 'static>>),
167
168    /// Failed to read current directory to discover workspace directory.
169    #[error("Failed to read current directory to discover workspace directory.")]
170    #[cfg_attr(
171        feature = "error_reporting",
172        diagnostic(code(peace_rt_model_native::working_dir_read))
173    )]
174    WorkingDirRead(#[source] std::io::Error),
175
176    /// Failed to create a workspace directory.
177    #[error("Failed to create workspace directory: `{path}`.", path = path.display())]
178    #[cfg_attr(
179        feature = "error_reporting",
180        diagnostic(code(peace_rt_model_native::workspace_dir_create))
181    )]
182    WorkspaceDirCreate {
183        /// The directory that was attempted to be created.
184        path: PathBuf,
185        /// Underlying IO error.
186        #[source]
187        error: std::io::Error,
188    },
189
190    /// Failed to determine workspace directory.
191    #[error(
192        "Failed to determine workspace directory as could not find `{file_name}` \
193            in `{working_dir}` or any parent directories.",
194        file_name = file_name.to_string_lossy(),
195        working_dir = working_dir.display())]
196    #[cfg_attr(
197        feature = "error_reporting",
198        diagnostic(code(peace_rt_model_native::workspace_file_not_found))
199    )]
200    WorkspaceFileNotFound {
201        /// Beginning directory of traversal.
202        working_dir: PathBuf,
203        /// File or directory name searched for.
204        file_name: OsString,
205    },
206}