superkeyloader_lib/lib.rs
1#[macro_use]
2pub extern crate log;
3
4#[macro_use]
5pub extern crate serde_derive;
6
7pub use exitfailure::ExitDisplay;
8pub use failure::ResultExt;
9
10pub mod github;
11
12pub use github as gh;
13
14///
15/// Handle HTTP status codes errors and "no SSH keys" error.
16///
17/// TODO: Add context about username and service to improve error messages
18///
19/// # Errors
20///
21/// The function use the crate `exitfailure` to print a better error message on exit.
22/// It returns an error string that contains the error description on:
23/// - Input vector length is 0
24/// - Input has no `Ok` result, but has error code:
25/// - `404` HTTP Status Code -> Usually it means that the user doesn't exists
26/// - `1001` Internal error -> GitHub username is invalid
27/// (see https://github.com/shinnn/github-username-regex)
28/// - `1002` Internal error -> GitHub API response could not be parsed
29/// - Every other status code -> Unrecognized HTTP status code (i.e. 500, 501, etc.)
30///
31/// > Assuming that a 2XX response will always have an `Ok` value, so it will never reach
32/// > 'error matching'
33///
34///
35/// # Examples
36///
37/// ## No errors
38///
39/// ```
40/// use superkeyloader_lib::error_handler_wrapper;
41///
42/// let data = vec!("key1".to_string(), "key1".to_string());
43/// let input = Ok(data.clone());
44/// let output = error_handler_wrapper(input);
45///
46/// assert_eq!(data, output.unwrap());
47/// ```
48///
49/// ## Missing user (404 status code)
50///
51/// ```
52/// use superkeyloader_lib::error_handler_wrapper;
53///
54/// let error_code: u16 = 404;
55/// let input = Err(error_code);
56/// let output = error_handler_wrapper(input);
57///
58/// assert!(output.is_err());
59///
60/// let expected_output = String::from("Wrong username");
61/// let error_message = output.err().unwrap();
62///
63/// assert!(error_message.contains(&expected_output));
64/// ```
65///
66pub fn error_handler_wrapper(res: Result<Vec<String>, u16>) -> Result<Vec<String>, String> {
67 match res {
68 Ok(res) => match res.len() {
69 0 => Err("User has no SSH keys available".into()),
70 _ => Ok(res),
71 },
72 Err(err) => match err {
73 404 => Err("Wrong username, doesn't exists".into()),
74 gh::INVALID_GH_API_RESPONSE => Err("Invalid GitHub API response".into()),
75 gh::INVALID_GH_USERNAME => {
76 Err(format!(
77 "Invalid username. Username isn't allowed on GitHub. \
78 If you think this is an bug, please create a issue on at {}/issues",
79 env!("CARGO_PKG_REPOSITORY")
80 )) // TODO: Maybe add this message to all error infos?
81 }
82 _ => Err(format!("API response code: {}", err)),
83 },
84 }
85}
86
87//
88// Testing
89//
90
91#[test]
92fn test_error_handling() {
93 // All Ok
94 let all_ok_input: Result<Vec<String>, u16> = Ok(vec!["key1".to_string(), "key2".to_string()]);
95 let all_ok_output: Result<Vec<String>, failure::Error> =
96 Ok(vec!["key1".to_string(), "key2".to_string()]);
97 assert_eq!(
98 error_handler_wrapper(all_ok_input).unwrap(),
99 all_ok_output.unwrap()
100 );
101
102 // No keys
103 let no_keys_input: Result<Vec<String>, u16> = Ok(vec![]);
104 assert_eq!(error_handler_wrapper(no_keys_input).is_err(), true);
105
106 // No user
107 let no_user_error_code: u16 = 404;
108 let no_user_input: Result<Vec<String>, u16> = Err(no_user_error_code);
109 assert_eq!(error_handler_wrapper(no_user_input).is_err(), true);
110
111 // Other error
112 let other_error_code: u16 = 500;
113 let other_error_input: Result<Vec<String>, u16> = Err(other_error_code);
114 assert_eq!(error_handler_wrapper(other_error_input).is_err(), true);
115
116 //
117 // GitHub Errors
118 //
119
120 // Invalid GitHub username
121 let invalid_user_input: Result<Vec<String>, u16> = Err(gh::INVALID_GH_USERNAME);
122 assert_eq!(error_handler_wrapper(invalid_user_input).is_err(), true);
123
124 // Invalid GitHub API Response
125 let invalid_user_input: Result<Vec<String>, u16> = Err(gh::INVALID_GH_API_RESPONSE);
126 assert_eq!(error_handler_wrapper(invalid_user_input).is_err(), true);
127}