test_dsl/
error.rs

1//! Common error definitions
2use miette::Diagnostic;
3use thiserror::Error;
4
5use crate::TestCaseInput;
6
7#[derive(Error, Diagnostic, Debug)]
8#[error("An error occurred while parsing testcases")]
9/// An error occurred while parsing testcases
10pub struct TestParseError {
11    #[related]
12    pub(crate) errors: Vec<TestErrorCase>,
13
14    #[source_code]
15    pub(crate) source_code: Option<TestCaseInput>,
16}
17
18impl From<kdl::KdlError> for TestParseError {
19    fn from(source: kdl::KdlError) -> Self {
20        TestParseError {
21            errors: vec![TestErrorCase::Kdl { source }],
22            source_code: None,
23        }
24    }
25}
26
27impl From<TestErrorCase> for TestParseError {
28    fn from(source: TestErrorCase) -> Self {
29        TestParseError {
30            errors: vec![source],
31            source_code: None,
32        }
33    }
34}
35
36#[derive(Error, Diagnostic, Debug)]
37/// Errors that can happen related to tests
38pub enum TestErrorCase {
39    /// KDL reported an error
40    #[error("An error occurred while parsing the KDL data")]
41    Kdl {
42        /// The specific KDL error
43        #[source]
44        source: kdl::KdlError,
45    },
46
47    /// An outer node was not a `testcase` node
48    #[error("Not a valid test case")]
49    #[diagnostic(help("The outer items must all be `testcase`s"))]
50    NotTestcase {
51        /// The location of the offending node
52        #[label("Expected a `testcase`")]
53        span: miette::SourceSpan,
54    },
55
56    /// An argument was missing from a node
57    #[error("An argument was missing")]
58    MissingArgument {
59        /// The location of the node
60        #[label("This node is missing an argument")]
61        parent: miette::SourceSpan,
62
63        /// Help related to what was missing
64        #[help]
65        missing: String,
66    },
67
68    /// A node had a wrong type in its parameter list
69    #[error("An argument was of the wrong type")]
70    WrongArgumentType {
71        /// The parent node
72        #[label("This node has an argument of a wrong kind")]
73        parent: miette::SourceSpan,
74
75        /// The offending argument
76        #[label("this one")]
77        argument: miette::SourceSpan,
78
79        /// Help text to explain what was expected, if possible
80        #[help]
81        expected: String,
82    },
83
84    /// The given condition could not be found
85    #[error("Could not find condition with this name")]
86    UnknownCondition {
87        /// The location of the condition node
88        #[label]
89        condition: miette::SourceSpan,
90    },
91
92    /// The given verb could not be found
93    #[error("Could not find verb with this name")]
94    UnknownVerb {
95        /// The location of the verb node
96        #[label]
97        verb: miette::SourceSpan,
98    },
99
100    /// The condition is not valid in this position
101    #[error("The condition is not valid in this position")]
102    InvalidCondition {
103        /// The inner error
104        #[diagnostic_source]
105        error: miette::Error,
106    },
107}
108
109#[derive(Debug, Error, Diagnostic)]
110/// Errors occurring while running tests
111pub enum TestError {
112    /// An error occurred in a verb/condition
113    #[error("An error occurred")]
114    Error {
115        #[diagnostic_source]
116        /// The returned error
117        error: miette::Error,
118
119        #[label("in this node")]
120        /// Which node caused the panic
121        span: miette::SourceSpan,
122    },
123
124    /// An panic occurred in a verb/condition
125    #[error("A panic occurred")]
126    Panic {
127        #[diagnostic_source]
128        /// The message of the panic if it had one
129        error: miette::Error,
130
131        #[label("in this node")]
132        /// Which node caused the panic
133        span: miette::SourceSpan,
134    },
135
136    /// The evaluated condition failed
137    #[error("The given condition failed")]
138    ConditionFailed {
139        #[label("in this node")]
140        /// Which node caused the panic
141        span: miette::SourceSpan,
142    },
143}