node_js_release_info/
error.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter, Result};
3
4#[derive(Debug)]
5pub enum NodeJSRelInfoError {
6    /// The operating system for the Node.js distributable you are targeting is
7    /// unrecognized - see: [`NodeJSOS`](crate::NodeJSOS) for options
8    UnrecognizedOs(String),
9    /// The CPU architecture for the Node.js distributable you are targeting is
10    /// unrecognized - see: [`NodeJSArch`](crate::NodeJSArch) for options
11    UnrecognizedArch(String),
12    /// The file extension of the Node.js distributable you are targeting is
13    /// unrecognized - see: [`NodeJSPkgExt`](crate::NodeJSPkgExt) for options
14    UnrecognizedExt(String),
15    /// The version string provided is invalid - see: [semver](https://semver.org)
16    InvalidVersion(String),
17    /// The version of Node.js you are targeting is not available
18    UnrecognizedVersion(String),
19    /// The Node.js configuration you are targeting is not available
20    UnrecognizedConfiguration(String),
21    /// Something went wrong issuing or processing the HTTP GET request to the Node.js [downloads server](https://nodejs.org/download/release/)
22    HttpError(reqwest::Error),
23}
24
25impl Error for NodeJSRelInfoError {}
26
27impl Display for NodeJSRelInfoError {
28    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
29        let message = match self {
30            NodeJSRelInfoError::UnrecognizedOs(input) => {
31                format!("Unrecognized OS! Received: '{}'", input)
32            }
33            NodeJSRelInfoError::UnrecognizedArch(input) => {
34                format!("Unrecognized Arch! Received: '{}'", input)
35            }
36            NodeJSRelInfoError::UnrecognizedExt(input) => {
37                format!("Unrecognized File Extension! Received: '{}'", input)
38            }
39            NodeJSRelInfoError::InvalidVersion(input) => {
40                format!("Invalid Version! Received: '{}'", input)
41            }
42            NodeJSRelInfoError::UnrecognizedVersion(input) => {
43                format!("Unrecognized Version! Received: '{}'", input)
44            }
45            NodeJSRelInfoError::UnrecognizedConfiguration(input) => {
46                format!("Unrecognized Configuration! Received: '{}'", input)
47            }
48            NodeJSRelInfoError::HttpError(e) => return write!(f, "{}", e),
49        };
50
51        write!(f, "Error: {}", message)
52    }
53}
54
55impl From<reqwest::Error> for NodeJSRelInfoError {
56    fn from(e: reqwest::Error) -> Self {
57        NodeJSRelInfoError::HttpError(e)
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn it_prints_expected_message_when_os_is_unrecognized() {
67        let err = NodeJSRelInfoError::UnrecognizedOs("unknown-os".to_string());
68        assert_eq!(
69            format!("{err}"),
70            "Error: Unrecognized OS! Received: 'unknown-os'"
71        );
72    }
73
74    #[test]
75    fn it_prints_expected_message_when_arch_is_unrecognized() {
76        let err = NodeJSRelInfoError::UnrecognizedArch("unknown-arch".to_string());
77        assert_eq!(
78            format!("{err}"),
79            "Error: Unrecognized Arch! Received: 'unknown-arch'"
80        );
81    }
82
83    #[test]
84    fn it_prints_expected_message_when_extension_is_unrecognized() {
85        let err = NodeJSRelInfoError::UnrecognizedExt("unknown-ext".to_string());
86        assert_eq!(
87            format!("{err}"),
88            "Error: Unrecognized File Extension! Received: 'unknown-ext'"
89        );
90    }
91
92    #[test]
93    fn it_prints_expected_message_when_version_is_invalid() {
94        let err = NodeJSRelInfoError::InvalidVersion("invalid-ver".to_string());
95        assert_eq!(
96            format!("{err}"),
97            "Error: Invalid Version! Received: 'invalid-ver'"
98        );
99    }
100
101    #[test]
102    fn it_prints_expected_message_when_version_is_unrecognized() {
103        let err = NodeJSRelInfoError::UnrecognizedVersion("unknown-ver".to_string());
104        assert_eq!(
105            format!("{err}"),
106            "Error: Unrecognized Version! Received: 'unknown-ver'"
107        );
108    }
109
110    #[test]
111    fn it_prints_expected_message_when_configuration_is_unrecognized() {
112        let err = NodeJSRelInfoError::UnrecognizedConfiguration("unknown-cfg".to_string());
113        assert_eq!(
114            format!("{err}"),
115            "Error: Unrecognized Configuration! Received: 'unknown-cfg'"
116        );
117    }
118
119    #[tokio::test]
120    async fn it_prints_expected_message_upon_http_error() {
121        let err = fake_http_error().await.unwrap_err();
122        assert_eq!(
123            format!("{err}"),
124            "builder error: relative URL without a base"
125        );
126    }
127
128    async fn fake_http_error() -> std::result::Result<(), NodeJSRelInfoError> {
129        let error = reqwest::get("not-a-url").await.unwrap_err();
130        Err(NodeJSRelInfoError::from(error))
131    }
132}