error_support/
macros.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5/// Tell the application to report an error
6///
7/// If configured by the application, this sent to the application, which should report it to a
8/// Sentry-like system. This should only be used for errors that we don't expect to see and will
9/// work on fixing if we see a non-trivial volume of them.
10///
11/// type_name identifies the error.  It should be the main text that gets shown to the
12/// user and also how the error system groups errors together.  It should be in UpperCamelCase
13/// form.
14///
15/// Good type_names require some trial and error, for example:
16///   - Start with the error kind variant name
17///   - Add more text to distinguish errors more.  For example an error code, or an extra word
18///     based on inspecting the error details
19#[macro_export]
20macro_rules! report_error {
21    ($type_name:expr, $($arg:tt)*) => {
22        let message = std::format!($($arg)*);
23        $crate::warn!("report {}: {}", $type_name, message);
24        $crate::report_error_to_app($type_name.to_string(), message.to_string());
25    };
26}
27
28/// Log a breadcrumb if we see an `Result::Err` value
29///
30/// Use this macro to wrap a function call that returns a `Result<>`.  If that call returns an
31/// error, then we will log a breadcrumb for it.  This can be used to track down the codepath where
32/// an error happened.
33#[macro_export]
34macro_rules! trace_error {
35    ($result:expr) => {{
36        let result = $result;
37        if let Err(e) = &result {
38            $crate::breadcrumb!("Saw error: {}", e);
39        };
40        result
41    }};
42}
43
44/// Tell the application to log a breadcrumb
45///
46/// Breadcrumbs are log-like entries that get tracked by the error reporting system.  When we
47/// report an error, recent breadcrumbs will be associated with it.
48#[macro_export]
49macro_rules! breadcrumb {
50    ($($arg:tt)*) => {
51        {
52            let message = std::format!($($arg)*);
53            $crate::info!("breadcrumb: {}", message);
54            $crate::report_breadcrumb(
55                message,
56                std::module_path!().to_string(),
57                std::line!(),
58                std::column!(),
59            );
60        }
61    };
62}