Skip to main content

xsd_parser/pipeline/parser/
error.rs

1use std::fmt::{Display, Formatter, Result as FmtResult};
2use std::io::Error as IoError;
3use std::path::PathBuf;
4
5use thiserror::Error;
6use url::{ParseError as UrlParseError, Url};
7
8use xsd_parser_types::misc::Namespace;
9use xsd_parser_types::quick_xml::Error as XmlError;
10
11use super::resolver::ResolveRequest;
12
13/// Represents the errors that are raised by the [`Parser`](super::Parser).
14#[derive(Debug, Error)]
15pub enum Error<E> {
16    /// An IO error occurred.
17    #[error("IO Error: {0}")]
18    IoError(#[from] IoError),
19
20    /// Error while interpreting the XML for the schema.
21    #[error("{0}")]
22    XmlError(#[from] XmlErrorWithLocation),
23
24    /// Error while paring the URL.
25    #[error("URL Parse Error: {0}")]
26    UrlParseError(#[from] UrlParseError),
27
28    /// Unable to resolve the requested resource.
29    #[error("Unable to resolve requested resource: {0}")]
30    UnableToResolve(Box<ResolveRequest>),
31
32    /// The target namespace of the included schema does not
33    /// match the target namespace of the including schema.
34    #[error(
35        "Mismatching target namespace (location={location}, found={found}, expected={expected})"
36    )]
37    MismatchingTargetNamespace {
38        /// Location of the schema that was processed.
39        location: Url,
40
41        /// Target namespace that was found in the included schema.
42        found: Namespace,
43
44        /// Target namespace that was expected (i.e. the target namespace of the including schema).
45        expected: Namespace,
46    },
47
48    /// Error while resolving the requested resource.
49    #[error("Resolver Error: {0}")]
50    Resolver(E),
51
52    /// Invalid file path!
53    ///
54    /// Is raised if the file path could not be converted to an [`Url`](url::Url).
55    #[error("Invalid file path: {0}!")]
56    InvalidFilePath(PathBuf),
57}
58
59impl<E> Error<E> {
60    /// Create a [`Error::Resolver`] from the passed `error`.
61    pub fn resolver<X: Into<E>>(error: X) -> Self {
62        Self::Resolver(error.into())
63    }
64}
65
66/// Error that is raised during deserialization of XSD structures.
67#[derive(Debug, Error)]
68pub struct XmlErrorWithLocation {
69    /// Error that was raised.
70    pub error: XmlError,
71
72    /// Location of the schema that was processed.
73    pub location: Option<Url>,
74}
75
76impl From<XmlError> for XmlErrorWithLocation {
77    fn from(error: XmlError) -> Self {
78        Self {
79            error,
80            location: None,
81        }
82    }
83}
84
85impl Display for XmlErrorWithLocation {
86    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
87        let Self { error, location } = self;
88
89        write!(f, "XML Error: ")?;
90        error.fmt(f)?;
91
92        if let Some(location) = location {
93            write!(f, "\n    in schema ")?;
94            location.fmt(f)?;
95        }
96
97        Ok(())
98    }
99}