Skip to main content

elemental_core/url/
error.rs

1use std::error::Error as StdError;
2
3#[derive(Debug, thiserror::Error)]
4#[non_exhaustive]
5pub enum OriginError {
6    #[error("{context}: {url}")]
7    InvalidUrl {
8        context: String,
9        url: String,
10        #[source]
11        source: Box<dyn StdError + Send + Sync>,
12    },
13
14    #[error("{context}: {base_url} + {path}")]
15    JoinUrl {
16        context: String,
17        base_url: String,
18        path: String,
19        #[source]
20        source: Box<dyn StdError + Send + Sync>,
21    },
22
23    #[error("invalid relative url path: '{path}'")]
24    InvalidRelativePath { path: String },
25
26    #[error("URL cannot be used as a path base: {url}")]
27    UrlNotPathBase { url: String },
28
29    #[error("can't map URL to a known origin: {raw_url}")]
30    UnknownOrigin { raw_url: String },
31
32    #[error("URL matches a known origin host but not a configured base path: {raw_url}")]
33    OriginBasePathMismatch { raw_url: String },
34
35    #[error("{context} must use http or https: {url}")]
36    UnsupportedUrlScheme { context: String, url: String },
37}
38
39impl OriginError {
40    pub fn invalid_url(
41        context: impl Into<String>,
42        url: impl Into<String>,
43        source: impl StdError + Send + Sync + 'static,
44    ) -> Self {
45        Self::InvalidUrl {
46            context: context.into(),
47            url: url.into(),
48            source: Box::new(source),
49        }
50    }
51
52    pub fn join_url(
53        context: impl Into<String>,
54        base_url: impl Into<String>,
55        path: impl Into<String>,
56        source: impl StdError + Send + Sync + 'static,
57    ) -> Self {
58        Self::JoinUrl {
59            context: context.into(),
60            base_url: base_url.into(),
61            path: path.into(),
62            source: Box::new(source),
63        }
64    }
65}
66
67pub type Result<T> = std::result::Result<T, OriginError>;