1
use super::payload::engine_error_code;
2
use super::*;
3
use crate::envelope::ErrorCode;
4

            
5
use scripting::runtime::EvalValue;
6

            
7
1
fn field_errors() -> EvalValue {
8
1
    EvalValue::List(vec![EvalValue::List(vec![
9
1
        EvalValue::Symbol(":FIELD".into()),
10
1
        EvalValue::String("a".into()),
11
1
        EvalValue::Symbol(":MESSAGE".into()),
12
1
        EvalValue::String("x".into()),
13
1
    ])])
14
1
}
15

            
16
2
fn host_condition(detail: Option<EvalValue>) -> EngineError {
17
2
    EngineError::HostCondition {
18
2
        code: "form-invalid".into(),
19
2
        message: "2 fields invalid".into(),
20
2
        detail,
21
2
    }
22
2
}
23

            
24
11
fn wire(err: EngineError) -> String {
25
11
    format_response(&EvalFailure::Engine(RequestId::Int(4), err).into_response())
26
11
}
27

            
28
#[test]
29
1
fn host_condition_code_is_passed_verbatim() {
30
1
    assert_eq!(
31
1
        engine_error_code(&host_condition(None)).as_symbol(),
32
1
        ErrorCode::FORM_INVALID
33
    );
34
1
}
35

            
36
#[test]
37
1
fn form_invalid_puts_the_field_errors_on_the_wire_as_a_list() {
38
1
    let err = crate::natives::forms::form_invalid(&[forms::FieldError {
39
1
        field: "a".into(),
40
1
        message: "x".into(),
41
1
    }]);
42
1
    assert_eq!(
43
1
        wire(scripting::runtime::classify_runtime_error(&err)),
44
1
        r#"(:id 4 :error (:code form-invalid :message "1 field invalid" :detail ((:FIELD "a" :MESSAGE "x"))))"#
45
    );
46
1
}
47

            
48
#[test]
49
1
fn host_condition_without_detail_has_no_detail() {
50
1
    assert_eq!(
51
1
        wire(host_condition(None)),
52
1
        r#"(:id 4 :error (:code form-invalid :message "2 fields invalid"))"#
53
    );
54
1
}
55

            
56
#[test]
57
1
fn other_engine_errors_keep_debug_detail_as_a_string() {
58
1
    let frame = wire(EngineError::Trap("boom".into()));
59
1
    assert!(frame.contains(r#":detail "Trap(\"boom\")""#), "{frame}");
60
1
}
61

            
62
2
fn script_raised(detail: Option<EvalValue>) -> EngineError {
63
2
    EngineError::ScriptRaised {
64
2
        code: "BOOM".into(),
65
2
        message: "bad".into(),
66
2
        detail,
67
2
    }
68
2
}
69

            
70
#[test]
71
1
fn script_raise_response_carries_its_detail_value() {
72
1
    assert_eq!(
73
1
        wire(script_raised(Some(field_errors()))),
74
1
        r#"(:id 4 :error (:code BOOM :message "script raised BOOM: bad" :detail ((:FIELD "a" :MESSAGE "x"))))"#
75
    );
76
1
}
77

            
78
#[test]
79
1
fn script_raise_without_detail_omits_it_from_the_frame() {
80
1
    assert_eq!(
81
1
        wire(script_raised(None)),
82
1
        r#"(:id 4 :error (:code BOOM :message "script raised BOOM: bad"))"#
83
    );
84
1
}
85

            
86
#[test]
87
1
fn a_thrown_host_condition_keeps_its_lower_case_code_and_plain_message() {
88
1
    let raised = EngineError::ScriptRaised {
89
1
        code: "NOT-FOUND".into(),
90
1
        message: "Commodity not found".into(),
91
1
        detail: Some(EvalValue::String("set-commodity-tag".into())),
92
1
    };
93
1
    assert_eq!(
94
1
        wire(raised),
95
1
        r#"(:id 4 :error (:code not-found :message "Commodity not found" :detail "set-commodity-tag"))"#
96
    );
97
1
}
98

            
99
#[test]
100
1
fn a_user_raise_of_a_host_code_goes_out_as_that_code() {
101
1
    let raised = EngineError::ScriptRaised {
102
1
        code: "NOT-FOUND".into(),
103
1
        message: "m".into(),
104
1
        detail: Some(EvalValue::String("x".into())),
105
1
    };
106
1
    assert_eq!(
107
1
        wire(raised),
108
1
        r#"(:id 4 :error (:code not-found :message "m" :detail "x"))"#
109
    );
110
1
}
111

            
112
#[test]
113
1
fn host_conditions_keep_their_wire_codes() {
114
1
    for code in [
115
1
        ErrorCode::BUSY,
116
1
        ErrorCode::NO_CONVERSION,
117
1
        ErrorCode::ARGS,
118
1
        ErrorCode::UNAVAILABLE,
119
1
    ] {
120
4
        let raised = EngineError::ScriptRaised {
121
4
            code: code.to_uppercase(),
122
4
            message: "m".into(),
123
4
            detail: None,
124
4
        };
125
4
        assert_eq!(
126
4
            wire(raised),
127
4
            format!(r#"(:id 4 :error (:code {code} :message "m"))"#)
128
        );
129
    }
130
1
}