Skip to main content

goliath_normalize/
error.rs

1//! Error types.
2
3use goliath_ocsf::schema::PathError as SchemaPathError;
4use goliath_rule::PathError;
5use goliath_sigma::YamlError;
6use thiserror::Error;
7
8/// A source definition that cannot be loaded.
9#[derive(Debug, Clone, PartialEq, Eq, Error)]
10#[non_exhaustive]
11pub enum DefinitionError {
12    /// The file is not acceptable YAML, or does not have the shape of a
13    /// source definition.
14    #[error(transparent)]
15    Yaml(#[from] YamlError),
16
17    /// A path into the OCSF event cannot be parsed.
18    #[error("kind `{kind}`: target `{target}`: {source}")]
19    Target {
20        /// The kind the field belongs to, or `common`.
21        kind: String,
22        /// The target as written.
23        target: String,
24        /// Why it cannot be parsed.
25        source: PathError,
26    },
27
28    /// A path into the decoded record cannot be parsed.
29    #[error("kind `{kind}`: source path `{path}` has an empty segment")]
30    SourcePath {
31        /// The kind the path belongs to, or `common`.
32        kind: String,
33        /// The path as written.
34        path: String,
35    },
36
37    /// Two fields write the same attribute, or one writes inside another,
38    /// so one of them would be lost.
39    #[error("kind `{kind}`: targets `{first}` and `{second}` overlap")]
40    Overlap {
41        /// The kind the fields belong to, with the common fields included.
42        kind: String,
43        /// One target.
44        first: String,
45        /// The other.
46        second: String,
47    },
48
49    /// A field writes an attribute the definition may not set directly.
50    #[error("kind `{kind}`: `{target}` is set by {reason}, not by a field")]
51    Reserved {
52        /// The kind the field belongs to, or `common`.
53        kind: String,
54        /// The target as written.
55        target: String,
56        /// What sets it instead.
57        reason: &'static str,
58    },
59
60    /// A kind has no condition, so it would accept every record and hide the
61    /// kinds after it.
62    #[error("kind `{kind}` has no `when` condition")]
63    Unconditional {
64        /// The kind.
65        kind: String,
66    },
67
68    /// The framing and the decoding cannot be used together.
69    #[error("framing `{framing}` cannot be used with decoding `{decoding}`")]
70    Decoding {
71        /// The framing as written.
72        framing: &'static str,
73        /// The decoding as written.
74        decoding: &'static str,
75    },
76
77    /// The definition has no kinds, so every record would be rejected.
78    #[error("the definition has no kinds")]
79    NoKinds,
80
81    /// A kind names a class the OCSF schema does not define.
82    #[error("kind `{kind}`: OCSF {version} has no class {class_uid}")]
83    UnknownClass {
84        /// The kind.
85        kind: String,
86        /// The `class_uid` as written.
87        class_uid: u32,
88        /// The schema version checked against.
89        version: &'static str,
90    },
91
92    /// A kind names an activity its class does not define.
93    #[error("kind `{kind}`: class `{class}` has no activity {activity_id}")]
94    UnknownActivity {
95        /// The kind.
96        kind: String,
97        /// The class name.
98        class: &'static str,
99        /// The `activity_id` as written.
100        activity_id: u32,
101    },
102
103    /// A target is not an attribute of the kind's class.
104    #[error("kind `{kind}`: {source}")]
105    Attribute {
106        /// The kind; a common field is reported for the first kind it fails
107        /// in.
108        kind: String,
109        /// Where the path left the schema.
110        source: SchemaPathError,
111    },
112
113    /// A field writes a value the attribute cannot hold.
114    #[error("kind `{kind}`: `{target}` holds {holds}, but the field writes {writes}")]
115    Type {
116        /// The kind; a common field is reported for the first kind it fails
117        /// in.
118        kind: String,
119        /// The target as written.
120        target: String,
121        /// What the attribute holds, such as `` `integer_t` ``.
122        holds: String,
123        /// What the field writes, such as `text`.
124        writes: &'static str,
125    },
126
127    /// A constant is not one of the values an enumerated attribute defines.
128    #[error("kind `{kind}`: {value} is not a defined value of `{target}`")]
129    UnknownValue {
130        /// The kind; a common field is reported for the first kind it fails
131        /// in.
132        kind: String,
133        /// The target as written.
134        target: String,
135        /// The constant.
136        value: i64,
137    },
138
139    /// A translation table is empty, or its values are not all of one type.
140    #[error("kind `{kind}`: the translation for `{target}` {reason}")]
141    Translation {
142        /// The kind the field belongs to, or `common`.
143        kind: String,
144        /// The target as written.
145        target: String,
146        /// What is wrong with the table.
147        reason: &'static str,
148    },
149
150    /// A target lies inside an array, so there is no one place to write it.
151    #[error("kind `{kind}`: `{target}` is inside an array, which a field cannot write into")]
152    WithinArray {
153        /// The kind; a common field is reported for the first kind it fails
154        /// in.
155        kind: String,
156        /// The target as written.
157        target: String,
158    },
159}