Skip to main content

sbo3l_core/
error.rs

1//! Error model. Mirrors ยง3 of `docs/spec/17_interface_contracts.md`.
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum SchemaError {
7    #[error("schema.unknown_field at {path}")]
8    UnknownField { path: String },
9    #[error("schema.missing_field {field}")]
10    MissingField { field: String },
11    #[error("schema.wrong_type at {path}: {detail}")]
12    WrongType { path: String, detail: String },
13    #[error("schema.value_out_of_range at {path}: {detail}")]
14    ValueOutOfRange { path: String, detail: String },
15    #[error("schema.invalid_root: {detail}")]
16    InvalidRoot { detail: String },
17}
18
19impl SchemaError {
20    pub fn code(&self) -> &'static str {
21        match self {
22            Self::UnknownField { .. } => "schema.unknown_field",
23            Self::MissingField { .. } => "schema.missing_field",
24            Self::WrongType { .. } => "schema.wrong_type",
25            Self::ValueOutOfRange { .. } => "schema.value_out_of_range",
26            Self::InvalidRoot { .. } => "schema.invalid_root",
27        }
28    }
29
30    pub fn http_status(&self) -> u16 {
31        400
32    }
33}
34
35#[derive(Debug, Error)]
36pub enum CoreError {
37    #[error(transparent)]
38    Schema(#[from] SchemaError),
39    #[error("canonicalization: {0}")]
40    Canonicalization(String),
41    #[error(transparent)]
42    Json(#[from] serde_json::Error),
43    #[error(transparent)]
44    Io(#[from] std::io::Error),
45}
46
47pub type Result<T> = std::result::Result<T, CoreError>;