diff options
Diffstat (limited to 'third_party/rust/error-support/src/macros.rs')
-rw-r--r-- | third_party/rust/error-support/src/macros.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/third_party/rust/error-support/src/macros.rs b/third_party/rust/error-support/src/macros.rs new file mode 100644 index 0000000000..cb95535c07 --- /dev/null +++ b/third_party/rust/error-support/src/macros.rs @@ -0,0 +1,75 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/// Tell the application to report an error +/// +/// If configured by the application, this sent to the application, which should report it to a +/// Sentry-like system. This should only be used for errors that we don't expect to see and will +/// work on fixing if we see a non-trivial volume of them. +/// +/// type_name identifies the error. It should be the main text that gets shown to the +/// user and also how the error system groups errors together. It should be in UpperCamelCase +/// form. +/// +/// Good type_names require some trial and error, for example: +/// - Start with the error kind variant name +/// - Add more text to distinguish errors more. For example an error code, or an extra word +/// based on inspecting the error details +#[macro_export] +macro_rules! report_error { + ($type_name:expr, $($arg:tt)*) => { + let message = std::format!($($arg)*); + ::log::warn!("report {}: {}", $type_name, message); + $crate::report_error_to_app($type_name.to_string(), message.to_string()); + }; +} + +/// Log a breadcrumb if we see an `Result::Err` value +/// +/// Use this macro to wrap a function call that returns a `Result<>`. If that call returns an +/// error, then we will log a breadcrumb for it. This can be used to track down the codepath where +/// an error happened. +#[macro_export] +macro_rules! trace_error { + ($result:expr) => {{ + let result = $result; + if let Err(e) = &result { + $crate::breadcrumb!("Saw error: {}", e); + }; + result + }}; +} + +/// Tell the application to log a breadcrumb +/// +/// Breadcrumbs are log-like entries that get tracked by the error reporting system. When we +/// report an error, recent breadcrumbs will be associated with it. +#[macro_export] +macro_rules! breadcrumb { + ($($arg:tt)*) => { + { + let message = std::format!($($arg)*); + ::log::info!("breadcrumb: {}", message); + $crate::report_breadcrumb( + message, + std::module_path!().to_string(), + std::line!(), + std::column!(), + ); + } + }; +} + +/// Function wrapper macro to convert from a component's internal errors to external errors +/// and optionally log and report the error. +#[macro_export] +macro_rules! handle_error { + { $($tt:tt)* } => { + let body = || { + $($tt)* + }; + let result: Result<_> = body(); + result.map_err($crate::convert_log_report_error) + } +} |