Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | use std::borrow::Cow; |
| 6 | use std::collections::BTreeMap; |
| 7 | |
| 8 | use serde::Serialize; |
| 9 | |
| 10 | #[doc(hidden)] |
| 11 | pub mod macro_internal { |
| 12 | use std::borrow::Cow; |
| 13 | use std::fmt::{self, Arguments, Display, Formatter}; |
| 14 | |
| 15 | pub use anyhow; |
| 16 | pub use arc_anyhow; |
| 17 | pub use std::format_args; |
| 18 | |
| 19 | /// An error that stores its format string as well as the formatted message. |
| 20 | #[derive(Debug, Clone)] |
| 21 | pub struct AttributedError { |
| 22 | pub fmt: Cow<'static, str>, |
| 23 | pub message: Cow<'static, str>, |
| 24 | } |
| 25 | |
| 26 | impl AttributedError { |
| 27 | pub fn new_static(fmt: &'static str, args: Arguments) -> arc_anyhow::Error { |
| 28 | arc_anyhow::Error::from(anyhow::Error::from(match args.as_str() { |
| 29 | // This format string has no parameters. |
| 30 | Some(s) => Self { fmt: Cow::Borrowed(s), message: Cow::Borrowed(s) }, |
| 31 | // This format string has parameters and must be formatted. |
| 32 | None => Self { fmt: Cow::Borrowed(fmt), message: Cow::Owned(fmt::format(args)) }, |
| 33 | })) |
| 34 | } |
| 35 | |
| 36 | pub fn new_dynamic(err: impl Display) -> arc_anyhow::Error { |
| 37 | // Use the whole error as the format string. This is preferable to |
| 38 | // grouping all dynamic errors under the "{}" format string. |
| 39 | let message = format!("{}", err); |
| 40 | arc_anyhow::Error::from(anyhow::Error::from(Self { |
| 41 | fmt: Cow::Owned(message.clone()), |
| 42 | message: Cow::Owned(message), |
| 43 | })) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl Display for AttributedError { |
| 48 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 49 | write!(f, "{}", self.message) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl std::error::Error for AttributedError {} |
| 54 | } |
| 55 | |
| 56 | use crate::macro_internal::AttributedError; |
| 57 | |
| 58 | /// Evaluates to an [`arc_anyhow::Error`]. |
| 59 | /// |
| 60 | /// Otherwise similar to [`anyhow::anyhow`]. |
| 61 | #[macro_export] |
| 62 | macro_rules! anyhow { |
| 63 | ($fmt:literal $(,)?) => { |
| 64 | $crate::macro_internal::AttributedError::new_static( |
| 65 | $fmt, |
| 66 | $crate::macro_internal::format_args!($fmt), |
| 67 | ) |
| 68 | }; |
| 69 | ($err:expr $(,)?) => { |
| 70 | $crate::macro_internal::AttributedError::new_dynamic($err) |
| 71 | }; |
| 72 | ($fmt:expr, $($arg:tt)*) => { |
| 73 | $crate::macro_internal::AttributedError::new_static( |
| 74 | $fmt, |
| 75 | $crate::macro_internal::format_args!($fmt, $($arg)*), |
| 76 | ) |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | /// Returns a [`Result::Err`] containing an [`arc_anyhow::Error`]. |
| 81 | /// |
| 82 | /// Otherwise similar to [`anyhow::bail`]. |
| 83 | #[macro_export] |
| 84 | macro_rules! bail { |
| 85 | ($fmt:literal $(,)?) => { |
| 86 | return Err($crate::anyhow!($fmt)) |
| 87 | }; |
| 88 | ($err:expr $(,)?) => { |
| 89 | return Err($crate::anyhow!($err)) |
| 90 | }; |
| 91 | ($fmt:expr, $($arg:tt)*) => { |
| 92 | return Err($crate::anyhow!($fmt, $($arg)*)) |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | /// Returns a [`Result::Err`] containing an [`arc_anyhow::Error`] if the given |
| 97 | /// condition evaluates to false. |
| 98 | /// |
| 99 | /// Otherwise similar to [`anyhow::ensure`]. |
| 100 | #[macro_export] |
| 101 | macro_rules! ensure { |
| 102 | ($cond:expr, $fmt:literal $(,)?) => { |
| 103 | if !$cond { bail!($fmt); } |
| 104 | }; |
| 105 | ($cond:expr, $err:expr $(,)?) => { |
| 106 | if !$cond { bail!($err); } |
| 107 | }; |
| 108 | ($cond:expr, $fmt:expr, $($arg:tt)*) => { |
| 109 | if !$cond { bail!($fmt, $($arg)*); } |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | pub trait ErrorReporting { |
| 114 | fn insert(&mut self, error: &arc_anyhow::Error); |
| 115 | fn serialize_to_vec(&self) -> anyhow::Result<Vec<u8>>; |
| 116 | } |
| 117 | |
| 118 | /// A null [`ErrorReporting`] strategy. |
| 119 | pub struct IgnoreErrors; |
| 120 | |
| 121 | impl ErrorReporting for IgnoreErrors { |
| 122 | fn insert(&mut self, _error: &arc_anyhow::Error) {} |
| 123 | |
| 124 | fn serialize_to_vec(&self) -> anyhow::Result<Vec<u8>> { |
| 125 | Ok(vec![]) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// An aggregate of zero or more errors. |
| 130 | #[derive(Default, Serialize)] |
| 131 | pub struct ErrorReport { |
| 132 | #[serde(flatten)] |
| 133 | map: BTreeMap<Cow<'static, str>, ErrorReportEntry>, |
| 134 | } |
| 135 | |
| 136 | impl ErrorReport { |
| 137 | pub fn new() -> Self { |
| 138 | Self::default() |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl ErrorReporting for ErrorReport { |
| 143 | fn insert(&mut self, error: &arc_anyhow::Error) { |
| 144 | if let Some(error) = error.downcast_ref::<AttributedError>() { |
| 145 | let sample_message = if error.message != error.fmt { &*error.message } else { "" }; |
| 146 | self.map.entry(error.fmt.clone()).or_default().add(Cow::Borrowed(sample_message)); |
| 147 | } else { |
| 148 | self.map.entry(Cow::Borrowed("{}")).or_default().add(Cow::Owned(format!("{error}"))); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | fn serialize_to_vec(&self) -> anyhow::Result<Vec<u8>> { |
| 153 | Ok(serde_json::to_vec(self)?) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #[derive(Default, Serialize)] |
| 158 | struct ErrorReportEntry { |
| 159 | count: u64, |
| 160 | #[serde(skip_serializing_if = "String::is_empty")] |
| 161 | sample_message: String, |
| 162 | } |
| 163 | |
| 164 | impl ErrorReportEntry { |
| 165 | fn add(&mut self, message: Cow<str>) { |
| 166 | if self.count == 0 { |
| 167 | self.sample_message = message.into_owned(); |
| 168 | } |
| 169 | self.count += 1; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | #[cfg(test)] |
| 174 | mod tests { |
| 175 | use super::*; |
| 176 | use std::borrow::Cow; |
| 177 | |
| 178 | #[test] |
| 179 | fn anyhow_1arg_static_plain() { |
| 180 | let arc_err = anyhow!("abc"); |
| 181 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 182 | assert!(matches!(err.fmt, Cow::Borrowed(_))); |
| 183 | assert_eq!(err.fmt, "abc"); |
| 184 | assert!(matches!(err.message, Cow::Borrowed(_))); |
| 185 | assert_eq!(err.message, "abc"); |
| 186 | } |
| 187 | |
| 188 | #[test] |
| 189 | fn anyhow_1arg_static_fmt() { |
| 190 | let some_var = "def"; |
| 191 | let arc_err = anyhow!("abc{some_var}"); |
| 192 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 193 | assert!(matches!(err.fmt, Cow::Borrowed(_))); |
| 194 | assert_eq!(err.fmt, "abc{some_var}"); |
| 195 | assert!(matches!(err.message, Cow::Owned(_))); |
| 196 | assert_eq!(err.message, "abcdef"); |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn anyhow_1arg_dynamic() { |
| 201 | let arc_err = anyhow!(format!("abc{}", "def")); |
| 202 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 203 | assert!(matches!(err.fmt, Cow::Owned(_))); |
| 204 | assert_eq!(err.fmt, "abcdef"); |
| 205 | assert!(matches!(err.message, Cow::Owned(_))); |
| 206 | assert_eq!(err.message, "abcdef"); |
| 207 | } |
| 208 | |
| 209 | #[test] |
| 210 | fn anyhow_2arg() { |
| 211 | let arc_err = anyhow!("abc{}", "def"); |
| 212 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 213 | assert!(matches!(err.fmt, Cow::Borrowed(_))); |
| 214 | assert_eq!(err.fmt, "abc{}"); |
| 215 | assert!(matches!(err.message, Cow::Owned(_))); |
| 216 | assert_eq!(err.message, "abcdef"); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn bail_1arg_static_plain() { |
| 221 | let arc_err = (|| -> arc_anyhow::Result<()> { bail!("abc") })().unwrap_err(); |
| 222 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 223 | assert!(matches!(err.fmt, Cow::Borrowed(_))); |
| 224 | assert_eq!(err.fmt, "abc"); |
| 225 | assert!(matches!(err.message, Cow::Borrowed(_))); |
| 226 | assert_eq!(err.message, "abc"); |
| 227 | } |
| 228 | |
| 229 | #[test] |
| 230 | fn bail_1arg_static_fmt() { |
| 231 | let some_var = "def"; |
| 232 | let arc_err = (|| -> arc_anyhow::Result<()> { bail!("abc{some_var}") })().unwrap_err(); |
| 233 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 234 | assert!(matches!(err.fmt, Cow::Borrowed(_))); |
| 235 | assert_eq!(err.fmt, "abc{some_var}"); |
| 236 | assert!(matches!(err.message, Cow::Owned(_))); |
| 237 | assert_eq!(err.message, "abcdef"); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn bail_1arg_dynamic() { |
| 242 | let arc_err = |
| 243 | (|| -> arc_anyhow::Result<()> { bail!(format!("abc{}", "def")) })().unwrap_err(); |
| 244 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 245 | assert!(matches!(err.fmt, Cow::Owned(_))); |
| 246 | assert_eq!(err.fmt, "abcdef"); |
| 247 | assert!(matches!(err.message, Cow::Owned(_))); |
| 248 | assert_eq!(err.message, "abcdef"); |
| 249 | } |
| 250 | |
| 251 | #[test] |
| 252 | fn bail_2arg() { |
| 253 | let arc_err = (|| -> arc_anyhow::Result<()> { bail!("abc{}", "def") })().unwrap_err(); |
| 254 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 255 | assert!(matches!(err.fmt, Cow::Borrowed(_))); |
| 256 | assert_eq!(err.fmt, "abc{}"); |
| 257 | assert!(matches!(err.message, Cow::Owned(_))); |
| 258 | assert_eq!(err.message, "abcdef"); |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn ensure_pass() { |
| 263 | let f = || { |
| 264 | ensure!(true, "unused message"); |
| 265 | Ok(()) |
| 266 | }; |
| 267 | f().unwrap(); |
| 268 | } |
| 269 | |
| 270 | #[test] |
| 271 | fn ensure_fail_1arg_static_plain() { |
| 272 | let arc_err = (|| { |
| 273 | ensure!(false, "abc"); |
| 274 | Ok(()) |
| 275 | })() |
| 276 | .unwrap_err(); |
| 277 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 278 | assert!(matches!(err.fmt, Cow::Borrowed(_))); |
| 279 | assert_eq!(err.fmt, "abc"); |
| 280 | assert!(matches!(err.message, Cow::Borrowed(_))); |
| 281 | assert_eq!(err.message, "abc"); |
| 282 | } |
| 283 | |
| 284 | #[test] |
| 285 | fn ensure_fail_1arg_static_fmt() { |
| 286 | let some_var = "def"; |
| 287 | let arc_err = (|| { |
| 288 | ensure!(false, "abc{some_var}"); |
| 289 | Ok(()) |
| 290 | })() |
| 291 | .unwrap_err(); |
| 292 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 293 | assert!(matches!(err.fmt, Cow::Borrowed(_))); |
| 294 | assert_eq!(err.fmt, "abc{some_var}"); |
| 295 | assert!(matches!(err.message, Cow::Owned(_))); |
| 296 | assert_eq!(err.message, "abcdef"); |
| 297 | } |
| 298 | |
| 299 | #[test] |
| 300 | fn ensure_fail_1arg_dynamic() { |
| 301 | let arc_err = (|| { |
| 302 | ensure!(false, format!("abc{}", "def")); |
| 303 | Ok(()) |
| 304 | })() |
| 305 | .unwrap_err(); |
| 306 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 307 | assert!(matches!(err.fmt, Cow::Owned(_))); |
| 308 | assert_eq!(err.fmt, "abcdef"); |
| 309 | assert!(matches!(err.message, Cow::Owned(_))); |
| 310 | assert_eq!(err.message, "abcdef"); |
| 311 | } |
| 312 | |
| 313 | #[test] |
| 314 | fn ensure_fail_2arg() { |
| 315 | let arc_err = (|| { |
| 316 | ensure!(false, "abc{}", "def"); |
| 317 | Ok(()) |
| 318 | })() |
| 319 | .unwrap_err(); |
| 320 | let err: &AttributedError = arc_err.downcast_ref().unwrap(); |
| 321 | assert!(matches!(err.fmt, Cow::Borrowed(_))); |
| 322 | assert_eq!(err.fmt, "abc{}"); |
| 323 | assert!(matches!(err.message, Cow::Owned(_))); |
| 324 | assert_eq!(err.message, "abcdef"); |
| 325 | } |
| 326 | |
| 327 | #[test] |
| 328 | fn error_report() { |
| 329 | let mut report = ErrorReport::new(); |
| 330 | report.insert(&anyhow!("abc{}", "def")); |
| 331 | report.insert(&anyhow!("abc{}", "123")); |
| 332 | report.insert(&anyhow!("error code: {}", 65535)); |
| 333 | report.insert(&anyhow!("no parameters")); |
| 334 | report.insert(&anyhow!("no parameters")); |
| 335 | report.insert(&anyhow!("no parameters")); |
| 336 | report.insert(&anyhow::Error::msg("not attributed").into()); |
| 337 | |
| 338 | assert_eq!( |
| 339 | serde_json::to_string_pretty(&report).unwrap(), |
| 340 | r#"{ |
| 341 | "abc{}": { |
| 342 | "count": 2, |
| 343 | "sample_message": "abcdef" |
| 344 | }, |
| 345 | "error code: {}": { |
| 346 | "count": 1, |
| 347 | "sample_message": "error code: 65535" |
| 348 | }, |
| 349 | "no parameters": { |
| 350 | "count": 3 |
| 351 | }, |
| 352 | "{}": { |
| 353 | "count": 1, |
| 354 | "sample_message": "not attributed" |
| 355 | } |
| 356 | }"#, |
| 357 | ); |
| 358 | } |
| 359 | } |