Skip to main content

ephemeris_forge_forgejo/api/
error.rs

1use ephemeris_forge::{EmptyUserName, TransportError, VariableNameError};
2
3/// Anything that stops a Forgejo request from producing a usable answer.
4#[derive(Debug, thiserror::Error)]
5pub enum ForgeError {
6    /// Transport trouble: unreachable, a bad credential, a missing resource,
7    /// a rate limit, a rejected body, or a body this build cannot read.
8    #[error(transparent)]
9    Transport(#[from] TransportError),
10    /// The repository rejects writes because it is archived.
11    #[error("the repository is archived, so this write is refused")]
12    Archived,
13    /// `POST .../branch_protections` answered 403 and the rule reads back, so
14    /// it is already there.
15    #[error("branch rule {name:?} already exists")]
16    RuleExists {
17        /// The branch rule name that already exists.
18        name: String,
19    },
20    /// An Actions variable name is not one Forgejo stores.
21    #[error(transparent)]
22    VariableName(#[from] VariableNameError),
23    /// A branch rule answered 403 on create (meaning it exists) but then
24    /// could not be read back to apply the recovering patch.
25    #[error("branch rule {name:?} appeared during create but could not be read back")]
26    RuleDisappeared {
27        /// The branch rule name that could not be read back.
28        name: String,
29    },
30    /// The forge answered the current user request with an empty login.
31    #[error("the forge answered the current user request with an empty login")]
32    EmptyCallerLogin(#[from] EmptyUserName),
33}
34
35#[cfg(test)]
36mod tests {
37    use ephemeris_forge::{RequestLabel, TransportError};
38    use reqwest::Method;
39
40    use super::ForgeError;
41
42    #[test]
43    fn transport_failures_are_wrapped_and_keep_their_message() {
44        let error: ForgeError = TransportError::NotFound {
45            request: RequestLabel::new(&Method::GET, "/x"),
46        }
47        .into();
48        assert!(matches!(error, ForgeError::Transport(_)));
49        assert!(error.to_string().contains("not there"));
50    }
51}