Skip to main content

geam_core/embedding/
error.rs

1use crate::ExecutionError;
2use thiserror::Error;
3
4/// A failure while calling a previously bound function.
5#[derive(Debug, Error, Clone, PartialEq)]
6pub enum CallError {
7    #[error("the function belongs to a different embedding module")]
8    ForeignFunction,
9    #[error("the retained value belongs to a different embedding module")]
10    ForeignValue,
11    #[error(transparent)]
12    Execution(#[from] ExecutionError),
13}
14
15#[cfg(test)]
16mod tests {
17    use super::CallError;
18    use crate::{ExecutionError, PanicKind, PanicSite, SourceSpan};
19
20    #[test]
21    fn displays_a_foreign_function_owner() {
22        let error = CallError::ForeignFunction;
23
24        assert_eq!(
25            error.to_string(),
26            "the function belongs to a different embedding module",
27        );
28        assert_eq!(error.clone(), error);
29
30        let error = CallError::ForeignValue;
31        assert_eq!(
32            error.to_string(),
33            "the retained value belongs to a different embedding module"
34        );
35        assert_eq!(error.clone(), error);
36    }
37
38    #[test]
39    fn transparently_displays_a_source_execution_failure() {
40        let execution = ExecutionError::source_panic(
41            None,
42            PanicKind::Panic,
43            Some("stopped".into()),
44            PanicSite::new("library".into(), "explode".into(), SourceSpan::new(44, 62)),
45        );
46        let error = CallError::Execution(execution.clone());
47
48        assert_eq!(error.to_string(), "panic: stopped");
49        assert_eq!(error, CallError::from(execution));
50    }
51}