The smokesignal.events web application
at main 2.1 kB view raw
1//! Smoke Signal Errors 2//! 3//! All errors are represented as a string in this format: 4//! 5//! "error-smokesignal-<domain>-<number> <message>: <details>" 6//! 7//! The first part containing the "error-" prefix, domain, and number, is used 8//! to uniquely identify the error. This standard code format is used to convey 9//! the source (easy for humans and agents to search for and reference) and 10//! localize. 11//! 12//! The "message" part contains a simple message in English explaining the 13//! error. When an error does not have a localized string, this message may be 14//! displayed to the user or client. The message cannot contain a ":" character 15//! because it is used to seperate the message from the error details. 16//! 17//! The last "details" part contains additional error details that is useful 18//! for logging or debugging. 19//! 20//! Errors are defined in source files that have the "_errors" suffix or in 21//! sub-packages named "errors". 22//! 23use std::string::ToString; 24 25pub(crate) fn expand_error<S: ToString>(err: S) -> (String, String) { 26 let err: String = err.to_string(); 27 if !err.starts_with("error-") { 28 panic!("incorrect error format: {err}") 29 } 30 let (error_code, extra) = match err.split_once(' ') { 31 Some((error_code, extra)) => (error_code, extra), 32 _ => return (err.to_string(), "".to_string()), 33 }; 34 35 match extra.split_once(':') { 36 Some((message, _)) => (error_code.to_string(), message.to_string()), 37 None => (error_code.to_string(), extra.to_string()), 38 } 39} 40 41#[cfg(test)] 42mod tests { 43 use super::*; 44 45 #[test] 46 fn test_expand_error() { 47 assert_eq!( 48 expand_error("error-example-1"), 49 ("error-example-1".to_string(), "".to_string()) 50 ); 51 assert_eq!( 52 expand_error("error-example-1 An example"), 53 ("error-example-1".to_string(), "An example".to_string()) 54 ); 55 assert_eq!( 56 expand_error("error-example-1 An example: With details"), 57 ("error-example-1".to_string(), "An example".to_string()) 58 ); 59 } 60}