1
use std::sync::{Arc, Mutex};
2

            
3
use crate::envelope::ErrorCode;
4
use scripting::runtime::{EngineOpts, build_engine, classify_runtime_error, compile_wat};
5
use uuid::Uuid;
6
use wasmtime::{Linker, Store};
7

            
8
use super::*;
9
use crate::host_failure::not_found_condition;
10
use crate::natives::native::register_native;
11

            
12
const WAT: &str = r#"
13
(module
14
  (import "nomi" "missing" (func $missing))
15
  (func (export "go") (call $missing)))
16
"#;
17

            
18
#[tokio::test(flavor = "current_thread")]
19
1
async fn a_condition_that_cannot_be_thrown_keeps_its_wire_code_and_plain_detail() {
20
1
    let engine = build_engine(EngineOpts::baseline()).unwrap();
21
1
    let mut linker: Linker<SessionData> = Linker::new(&engine);
22
1
    register_native(&mut linker, "missing", |_caller, ()| {
23
1
        Box::pin(async {
24
1
            Err::<(), _>(not_found_condition(
25
1
                "Account not found",
26
1
                Some("set-account-tag".into()),
27
1
            ))
28
1
        })
29
1
    })
30
    .unwrap();
31
1
    let module = compile_wat(&engine, WAT).unwrap();
32
1
    let data = SessionData::new(
33
1
        ScriptCtx::new(Uuid::nil()),
34
1
        Arc::new(Mutex::new(String::new())),
35
1
    );
36
1
    let mut store = Store::new(&engine, data);
37
1
    store.set_epoch_deadline(1_000);
38
1
    let instance = linker.instantiate_async(&mut store, &module).await.unwrap();
39
1
    let go = instance.get_func(&mut store, "go").unwrap();
40
1
    let err = go.call_async(&mut store, &[], &mut []).await.unwrap_err();
41

            
42
    let response =
43
1
        EvalFailure::Engine(RequestId::Int(11), classify_runtime_error(&err)).into_response();
44

            
45
1
    assert_eq!(
46
1
        response.payload,
47
1
        ResponsePayload::Error {
48
1
            code: ErrorCode::new("not-found"),
49
1
            message: "Account not found".into(),
50
1
            detail: Some(Value::String("set-account-tag".into())),
51
1
        }
52
1
    );
53
1
}