summaryrefslogtreecommitdiffstats
path: root/third_party/rust/error-support/src/macros.rs
blob: cb95535c07d22710ac120410fcc65bbc3ab3a530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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)
    }
}