Improve timeago/date filter error handling

This commit is contained in:
Luke Street
2025-04-08 08:23:03 -06:00
parent a7bf884c8b
commit b435313c66
+11 -6
View File
@@ -31,15 +31,20 @@ where S: serde::Serialize {
} }
fn timeago(value: String) -> String { fn timeago(value: String) -> String {
let value = DateTime::parse_from_rfc3339(&value).unwrap_or_default(); let Ok(value) = DateTime::parse_from_rfc3339(&value) else {
let duration = Utc::now().signed_duration_since(value); return "[invalid]".to_string();
timeago::Formatter::new().convert(duration.to_std().unwrap()) };
let Ok(duration) = Utc::now().signed_duration_since(value).to_std() else {
return "[out of range]".to_string();
};
timeago::Formatter::new().convert(duration)
} }
fn date(value: String, format: Option<String>) -> String { fn date(value: String, format: Option<String>) -> String {
let value = DateTime::parse_from_rfc3339(&value).unwrap_or_default(); let Ok(value) = DateTime::parse_from_rfc3339(&value) else {
let format = format.as_deref().unwrap_or("%Y-%m-%d %H:%M:%S %Z"); return "[invalid]".to_string();
value.format(format).to_string() };
value.format(format.as_deref().unwrap_or("%Y-%m-%d %H:%M:%S %Z")).to_string()
} }
/// Format a size in bytes to a human-readable string. /// Format a size in bytes to a human-readable string.