crashtest/
lib.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//! # Crash Test Helper APIs
6//!
7//! The `crashtest` component offers a little helper API that lets you deliberately
8//! crash the application. It's intended to help developers test the crash-handling
9//! and crash-reporting capabilities of their app.
10
11// Temporary, to work around a clippy lint in generated code.
12// https://github.com/mozilla/uniffi-rs/issues/1018
13#![allow(clippy::redundant_closure)]
14
15use thiserror::Error;
16
17#[cfg(test)]
18mod tests;
19
20uniffi::include_scaffolding!("crashtest");
21
22/// Trigger a hard abort inside the Rust code.
23///
24/// This function simulates some kind of uncatchable illegal operation
25/// performed inside the Rust code. After calling this function you should
26/// expect your application to be halted with e.g. a `SIGABRT` or similar.
27///
28pub fn trigger_rust_abort() {
29    log::error!("Now triggering an abort inside the Rust code");
30    std::process::abort();
31}
32
33/// Trigger a panic inside the Rust code.
34///
35/// This function simulates the occurrence of an unexpected state inside
36/// the Rust code that causes it to panic. We build our Rust components to
37/// unwind on panic, so after calling this function through the foreign
38/// language bindings, you should expect it to intercept the panic translate
39/// it into some foreign-language-appropriate equivalent:
40///
41///  - In Kotlin, it will throw an exception.
42///  - In Swift, it will fail with a `try!` runtime error.
43///
44pub fn trigger_rust_panic() {
45    log::error!("Now triggering a panic inside the Rust code");
46    panic!("Panic! In The Rust Code.");
47}
48
49/// Trigger an error inside the Rust code.
50///
51/// This function simulates the occurrence of an expected error inside
52/// the Rust code. You should expect calling this function to throw the
53/// foreign-language representation of the [`CrashTestError`] class.
54///
55pub fn trigger_rust_error() -> Result<(), CrashTestError> {
56    log::error!("Now triggering an error inside the Rust code");
57    Err(CrashTestError::ErrorFromTheRustCode)
58}
59
60/// An error that can be returned from Rust code.
61///
62#[derive(Debug, Error)]
63pub enum CrashTestError {
64    #[error("Error! From The Rust Code.")]
65    ErrorFromTheRustCode,
66}