1
//! Structured condition detail (EXD): `(error 'code "msg" detail)` end to end through
2
//! the `Session`, the wire frame and `catch-each`.
3

            
4
use super::*;
5
use crate::envelope::format_response;
6

            
7
9
async fn frame(source: &str) -> String {
8
9
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
9
9
    let outcome = session.handle_request(&format!("(progn {source})")).await;
10
9
    format_response(&Response {
11
9
        stream: 0,
12
9
        id: RequestId::Int(1),
13
9
        payload: outcome.payload,
14
9
    })
15
9
}
16

            
17
#[tokio::test]
18
1
async fn an_uncaught_raise_with_a_plist_detail_puts_it_on_the_wire() {
19
1
    assert_eq!(
20
1
        frame(r#"(error 'boom "bad input" (list :field "a" :count 3))"#).await,
21
1
        concat!(
22
1
            r#"(:id 1 :error (:code BOOM :message "script raised BOOM: bad input" "#,
23
1
            r#":detail (:FIELD "a" :COUNT 3)))"#
24
1
        )
25
1
    );
26
1
}
27

            
28
#[tokio::test]
29
1
async fn an_uncaught_raise_without_detail_has_no_detail_on_the_wire() {
30
1
    assert_eq!(
31
1
        frame(r#"(error 'boom "bad input")"#).await,
32
1
        r#"(:id 1 :error (:code BOOM :message "script raised BOOM: bad input"))"#
33
1
    );
34
1
}
35

            
36
#[tokio::test]
37
1
async fn a_scalar_detail_is_printed_as_its_value() {
38
1
    let wire = frame(r#"(error 'boom "m" 42)"#).await;
39
1
    assert!(wire.ends_with(" :detail 42))"), "{wire}");
40
1
}
41

            
42
#[tokio::test]
43
1
async fn a_caught_detail_is_the_plist() {
44
1
    let payload = frame(
45
1
        r#"(handler-case (error 'boom "m" (list :field "a" :count 3))
46
1
             (boom (e) (getf (error-detail e) :count)))"#,
47
1
    )
48
1
    .await;
49
1
    assert_eq!(payload, "(:id 1 :value 3)");
50
1
}
51

            
52
#[tokio::test]
53
1
async fn the_catch_each_err_cell_carries_code_and_message_only() {
54
1
    let wire = frame(r#"(catch-each (list 1) x (error 'boom "bad" (list :n x)))"#).await;
55
1
    assert_eq!(wire, r#"(:id 1 :value ((ERR BOOM "bad")))"#);
56
1
}
57

            
58
#[tokio::test]
59
1
async fn a_four_argument_raise_is_an_arity_error() {
60
1
    let wire = frame(r#"(error 'boom "m" nil nil)"#).await;
61
1
    assert!(wire.contains(":code args"), "{wire}");
62
1
}
63

            
64
3
fn deep_detail_raise(levels: usize) -> String {
65
3
    let bindings: String = (0..levels)
66
223
        .map(|n| {
67
223
            let inner = if n == 0 {
68
3
                "1".to_string()
69
            } else {
70
220
                format!("v{}", n - 1)
71
            };
72
223
            format!("(v{n} (list {inner})) ")
73
223
        })
74
        .collect();
75
3
    format!(
76
3
        r#"(let* ({bindings}) (error 'deep "too nested" v{}))"#,
77
3
        levels - 1
78
    )
79
3
}
80

            
81
#[tokio::test]
82
1
async fn a_detail_nested_past_the_depth_limit_prints_the_placeholder() {
83
1
    let wire = frame(&deep_detail_raise(100)).await;
84
1
    assert!(
85
1
        wire.starts_with("(:id 1 :error (:code DEEP :message "),
86
        "{wire}"
87
    );
88
1
    assert!(wire.contains("too nested"), "{wire}");
89
1
    assert!(wire.contains("#<...>"), "{wire}");
90
1
    assert!(!wire.contains("undecodable"), "{wire}");
91
1
}
92

            
93
2
async fn deep_detail_payload(levels: usize) -> ResponsePayload {
94
2
    let wire = frame(&deep_detail_raise(levels)).await;
95
2
    crate::parse_response(&wire)
96
        .unwrap_or_else(|e| panic!("client cannot read {levels} levels: {e}\n{wire}"))
97
        .payload
98
2
}
99

            
100
2
fn assert_code_and_message(payload: &ResponsePayload) {
101
2
    assert!(
102
2
        matches!(payload, ResponsePayload::Error { code, message, .. }
103
2
            if code.as_symbol() == "deep" && message.contains("too nested")),
104
        "{payload:?}"
105
    );
106
2
}
107

            
108
#[tokio::test]
109
1
async fn a_detail_one_below_the_ceiling_is_read_back_structured() {
110
1
    let payload = deep_detail_payload(scripting::MAX_WIRE_LIST_DEPTH - 1).await;
111
1
    assert_code_and_message(&payload);
112
1
    assert!(
113
1
        matches!(
114
1
            &payload,
115
1
            ResponsePayload::Error {
116
1
                detail: Some(Value::Pair(_)),
117
1
                ..
118
1
            }
119
1
        ),
120
1
        "{payload:?}"
121
1
    );
122
1
}
123

            
124
#[tokio::test]
125
1
async fn a_detail_at_the_ceiling_keeps_its_structure_and_prints_the_placeholder() {
126
1
    let payload = deep_detail_payload(scripting::MAX_WIRE_LIST_DEPTH).await;
127
1
    assert_code_and_message(&payload);
128
    let ResponsePayload::Error {
129
        detail: Some(detail),
130
        ..
131
1
    } = &payload
132
    else {
133
        panic!("the raise carries a detail: {payload:?}");
134
    };
135
1
    assert!(matches!(detail, Value::Pair(_)), "{detail:?}");
136
1
    let printed = nomiscript::format_value(detail);
137
1
    assert!(printed.contains("#<...>"), "{printed}");
138
1
    assert!(!printed.contains("undecodable"), "{printed}");
139
1
}