Skip to main content

systemprompt_files/
error.rs

1//! Typed error surface for the `systemprompt-files` crate.
2//!
3//! Boilerplate variants (`Repository`, `Io`, `Json`, `Yaml`, `Validation`,
4//! `NotFound`, `Config`) are injected by [`systemprompt_models::domain_error`].
5//! Database errors funnel through the workspace's single
6//! [`systemprompt_traits::RepositoryError`] rather than `sqlx::Error`
7//! directly so the layer boundary is preserved.
8//!
9//! Copyright (c) systemprompt.io — Business Source License 1.1.
10//! See <https://systemprompt.io> for licensing details.
11
12use std::path::PathBuf;
13
14use systemprompt_config::ProfileBootstrapError;
15use systemprompt_identifiers::FileId;
16use systemprompt_models::domain_error;
17
18domain_error! {
19    pub enum FilesError {
20        common: [repository, io, json, yaml, validation, not_found, config],
21
22        #[error("profile: {0}")]
23        Profile(#[from] ProfileBootstrapError),
24
25        #[error("Failed to read files.yaml ({path:?}): {source}")]
26        ConfigRead {
27            path: PathBuf,
28            #[source]
29            source: std::io::Error,
30        },
31
32        #[error("Failed to parse files.yaml ({path:?}): {source}")]
33        ConfigParse {
34            path: PathBuf,
35            #[source]
36            source: serde_yaml::Error,
37        },
38
39        #[error("invalid UUID for file id {id}")]
40        InvalidFileId {
41            id: FileId,
42            #[source]
43            source: uuid::Error,
44        },
45    }
46}
47
48impl From<sqlx::Error> for FilesError {
49    fn from(err: sqlx::Error) -> Self {
50        Self::Repository(systemprompt_traits::RepositoryError::from(err))
51    }
52}
53
54pub type FilesResult<T> = Result<T, FilesError>;
55
56pub(crate) fn parse_file_uuid(id: &FileId) -> FilesResult<uuid::Uuid> {
57    uuid::Uuid::parse_str(id.as_str()).map_err(|source| FilesError::InvalidFileId {
58        id: id.clone(),
59        source,
60    })
61}