| 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 |
use parking_lot::RwLock; |
| 6 |
use std::sync::atomic::{AtomicU32, Ordering}; |
| 7 |
|
| 8 |
/// Counter for breadcrumb messages |
| 9 |
/// |
| 10 |
/// We are currently seeing breadcrumbs that may indicate that the reporting is unreliable. In |
| 11 |
/// some reports, the breadcrumbs seem like they may be duplicated and/or out of order. This |
| 12 |
/// counter is a temporary measure to check out that theory. |
| 13 |
static BREADCRUMB_COUNTER: AtomicU32 = AtomicU32::new(0); |
| 14 |
|
| 15 |
fn get_breadcrumb_counter_value() -> u32 { |
| 16 |
// Notes: |
| 17 |
// - fetch_add is specified to wrap around in case of overflow, which seems okay. |
| 18 |
// - By itself, this does not guarantee that breadcrumb logs will be ordered the same way as |
| 19 |
// the counter values. If two threads are running at the same time, it's very possible |
| 20 |
// that thread A gets the lower breadcrumb value, but thread B wins the race to report its |
| 21 |
// breadcrumb. However, if we expect operations to be synchronized, like with places DB, |
| 22 |
// then the breadcrumb counter values should always increase by 1. |
| 23 |
BREADCRUMB_COUNTER.fetch_add(1, Ordering::Relaxed) |
| 24 |
} |
| 25 |
|
| 26 |
/// Application error reporting trait |
| 27 |
/// |
| 28 |
/// The application that's consuming application-services implements this via a UniFFI callback |
| 29 |
/// interface, then calls `set_application_error_reporter()` to setup a global |
| 30 |
/// ApplicationErrorReporter. |
| 31 |
#[uniffi::export(callback_interface)] |
| 32 |
pub trait ApplicationErrorReporter: Sync + Send { |
| 33 |
/// Send an error report to a Sentry-like error reporting system |
| 34 |
/// |
| 35 |
/// type_name should be used to group errors together |
| 36 |
fn report_error(&self, type_name: String, message: String); |
| 37 |
/// Send a breadcrumb to a Sentry-like error reporting system |
| 38 |
fn report_breadcrumb(&self, message: String, module: String, line: u32, column: u32); |
| 39 |
} |
| 40 |
|
| 41 |
// ApplicationErrorReporter to use if the app doesn't set one |
| 42 |
struct DefaultApplicationErrorReporter; |
| 43 |
impl ApplicationErrorReporter for DefaultApplicationErrorReporter { |
| 44 |
fn report_error(&self, _type_name: String, _message: String) {} |
| 45 |
fn report_breadcrumb(&self, _message: String, _module: String, _line: u32, _column: u32) {} |
| 46 |
} |
| 47 |
|
| 48 |
lazy_static::lazy_static! { |
| 49 |
// RwLock rather than a Mutex, since we only expect to set this once. |
| 50 |
pub(crate) static ref APPLICATION_ERROR_REPORTER: RwLock<Box<dyn ApplicationErrorReporter>> = RwLock::new(Box::new(DefaultApplicationErrorReporter)); |
| 51 |
} |
| 52 |
|
| 53 |
/// Set the global error reporter. This is typically done early in startup. |
| 54 |
#[uniffi::export] |
| 55 |
pub fn set_application_error_reporter(error_reporter: Box<dyn ApplicationErrorReporter>) { |
| 56 |
*APPLICATION_ERROR_REPORTER.write() = error_reporter; |
| 57 |
} |
| 58 |
|
| 59 |
/// Unset the global error reporter. This is typically done at shutdown for |
| 60 |
/// platforms that want to cleanup references like Desktop. |
| 61 |
#[uniffi::export] |
| 62 |
pub fn unset_application_error_reporter() { |
| 63 |
*APPLICATION_ERROR_REPORTER.write() = Box::new(DefaultApplicationErrorReporter) |
| 64 |
} |
| 65 |
|
| 66 |
pub fn report_error_to_app(type_name: String, message: String) { |
| 67 |
APPLICATION_ERROR_REPORTER |
| 68 |
.read() |
| 69 |
.report_error(type_name, message); |
| 70 |
} |
| 71 |
|
| 72 |
pub fn report_breadcrumb(message: String, module: String, line: u32, column: u32) { |
| 73 |
let message = format!("{} ({})", message, get_breadcrumb_counter_value()); |
| 74 |
APPLICATION_ERROR_REPORTER |
| 75 |
.read() |
| 76 |
.report_breadcrumb(message, module, line, column); |
| 77 |
} |