test_dsl/
error.rs

1//! Common error definitions
2use std::sync::Arc;
3
4use miette::Diagnostic;
5use thiserror::Error;
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<miette::NamedSource<Arc<str>>>,
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    #[error("An error occurred while parsing the KDL data")]
40    /// KDL reported an error
41    Kdl {
42        #[source]
43        /// The specific KDL error
44        source: kdl::KdlError,
45    },
46    #[error("Not a valid test case")]
47    #[diagnostic(help("The outer items must all be `testcase`s"))]
48    /// An outer node was not a `testcase` node
49    NotTestcase {
50        #[label("Expected a `testcase`")]
51        /// The location of the offending node
52        span: miette::SourceSpan,
53    },
54    #[error("An argument was missing")]
55    /// An argument was missing from a node
56    MissingArgument {
57        #[label("This node is missing an argument")]
58        /// The location of the node
59        parent: miette::SourceSpan,
60
61        #[help]
62        /// Help related to what was missing
63        missing: String,
64    },
65    #[error("An argument was of the wrong type")]
66    /// A node had a wrong type in its parameter list
67    WrongArgumentType {
68        #[label("This node has an argument of a wrong kind")]
69        /// The parent node
70        parent: miette::SourceSpan,
71
72        #[label("this one")]
73        /// The offending argument
74        argument: miette::SourceSpan,
75
76        #[help]
77        /// Help text to explain what was expected, if possible
78        expected: String,
79    },
80    #[error("Could not find condition with this name")]
81    /// The given condition could not be found
82    UnknownCondition {
83        #[label]
84        /// The location of the condition node
85        condition: miette::SourceSpan,
86    },
87    #[error("Could not find verb with this name")]
88    /// The given verb could not be found
89    UnknownVerb {
90        #[label]
91        /// The location of the verb node
92        verb: miette::SourceSpan,
93    },
94
95    #[error("A panic occurred while running the test")]
96    /// An panic occurred while running a test
97    Panic {
98        #[diagnostic_source]
99        /// The message of the panic if it had one
100        error: miette::Error,
101
102        #[label("in this node")]
103        /// Which node caused the panic
104        label: miette::SourceSpan,
105    },
106
107    #[error("An error occurred while running the test")]
108    /// An error occurred in a node
109    Error {
110        #[diagnostic_source]
111        /// The error as it was given
112        error: miette::Error,
113
114        #[label("in this node")]
115        /// Which node cause the error
116        label: miette::SourceSpan,
117    },
118}