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
//! Wrapper for Dart exceptions.

use dart_sys::Dart_Handle;
use derive_more::Display;

use super::utils::handle::DartHandle;

/// Wrapper for Dart exception thrown when calling Dart code.
#[derive(Clone, Debug, Display, Eq, PartialEq)]
#[display(fmt = "DartPlatformError")]
pub struct Error(DartHandle);

impl Error {
    /// Creates a Dart [`Error`] out of the provided [`Dart_Handle`] from Dart
    /// side.
    ///
    /// # Safety
    ///
    /// The provided [`Dart_Handle`] should be non-`null` and point to the
    /// correct Dart exception.
    #[must_use]
    pub unsafe fn from_handle(h: Dart_Handle) -> Self {
        Self(DartHandle::new(h))
    }

    /// Returns a [`Dart_Handle`] to the underlying error.
    #[must_use]
    pub fn get_handle(&self) -> Dart_Handle {
        self.0.get()
    }

    /// Returns name of the underlying Dart exception.
    #[must_use]
    pub fn name(&self) -> String {
        self.0.name()
    }

    /// Returns message of the underlying Dart exception.
    #[must_use]
    pub fn message(&self) -> String {
        self.0.to_string()
    }
}