medea_jason/platform/dart/
error.rs

1//! Wrapper for Dart exceptions.
2
3use dart_sys::Dart_Handle;
4use derive_more::with_trait::Display;
5
6use super::utils::handle::DartHandle;
7
8/// Wrapper for Dart exception thrown when calling Dart code.
9#[derive(Clone, Debug, Display, Eq, PartialEq)]
10#[display("DartPlatformError")]
11pub struct Error(DartHandle);
12
13impl Error {
14    /// Creates a Dart [`Error`] out of the provided [`Dart_Handle`] from Dart
15    /// side.
16    ///
17    /// # Safety
18    ///
19    /// The provided [`Dart_Handle`] should be non-`null` and point to the
20    /// correct Dart exception.
21    #[must_use]
22    pub unsafe fn from_handle(h: Dart_Handle) -> Self {
23        Self(unsafe { DartHandle::new(h) })
24    }
25
26    /// Returns a [`Dart_Handle`] to the underlying error.
27    #[must_use]
28    pub fn get_handle(&self) -> Dart_Handle {
29        self.0.get()
30    }
31
32    /// Returns name of the underlying Dart exception.
33    #[must_use]
34    pub fn name(&self) -> String {
35        self.0.name()
36    }
37
38    /// Returns message of the underlying Dart exception.
39    #[must_use]
40    pub fn message(&self) -> String {
41        self.0.to_string()
42    }
43}