1
//! Mapping of engine, envelope and compiler errors to the wire error payload.
2

            
3
use nomiscript::{Error as NomiError, Value};
4
use scripting::runtime::EngineError;
5

            
6
use crate::envelope::{EnvelopeError, ErrorCode, ResponsePayload};
7

            
8
12288
pub(super) fn engine_payload(err: EngineError) -> ResponsePayload {
9
12288
    ResponsePayload::Error {
10
12288
        code: engine_error_code(&err),
11
12288
        message: err.to_string(),
12
12288
        detail: engine_error_detail(err),
13
12288
    }
14
12288
}
15

            
16
/// A native's thrown condition reaches the boundary as an upper-case `ScriptRaised`;
17
/// the wire keeps the lower-case host code and the plain message.
18
13095
pub(crate) fn restore_host_condition(err: EngineError) -> EngineError {
19
13095
    match err {
20
        EngineError::ScriptRaised {
21
            code,
22
            message,
23
            detail,
24
12430
        } if ErrorCode::HOST_CONDITIONS.contains(&code.to_lowercase().as_str()) => {
25
10791
            EngineError::HostCondition {
26
10791
                code: code.to_lowercase(),
27
10791
                message,
28
10791
                detail,
29
10791
            }
30
        }
31
2304
        other => other,
32
    }
33
13095
}
34

            
35
21781
pub(super) fn envelope_error_code(err: &EnvelopeError) -> ErrorCode {
36
21781
    let symbol = match err {
37
        EnvelopeError::Parse(_)
38
        | EnvelopeError::NotSingleExpr
39
        | EnvelopeError::NotPlist
40
        | EnvelopeError::InvalidStream(..)
41
1297
        | EnvelopeError::Unsupported => ErrorCode::PARSE,
42
20484
        EnvelopeError::MissingKey(_) | EnvelopeError::InvalidValue(_, _) => ErrorCode::ARGS,
43
    };
44
21781
    ErrorCode::new(symbol)
45
21781
}
46

            
47
1807
pub(super) fn nomi_error_code(err: &NomiError) -> ErrorCode {
48
1807
    let symbol = match err {
49
5
        NomiError::Parse(_) => ErrorCode::PARSE,
50
        NomiError::Compile(_)
51
        | NomiError::Resource(_)
52
        | NomiError::Dimension(_)
53
        | NomiError::Transfer { .. }
54
        | NomiError::UnclosableFactory { .. }
55
1787
        | NomiError::UndefinedSymbol(_) => ErrorCode::COMPILE,
56
        NomiError::Runtime(_) => ErrorCode::RUNTIME,
57
15
        NomiError::Type { .. } | NomiError::Arity { .. } => ErrorCode::ARGS,
58
    };
59
1807
    ErrorCode::new(symbol)
60
1807
}
61

            
62
12288
fn engine_error_detail(err: EngineError) -> Option<Value> {
63
12288
    match err {
64
        EngineError::HostCondition { detail, .. } | EngineError::ScriptRaised { detail, .. } => {
65
11625
            detail.map(Value::from)
66
        }
67
663
        other => Some(Value::String(format!("{other:?}"))),
68
    }
69
12288
}
70

            
71
12289
pub(super) fn engine_error_code(err: &EngineError) -> ErrorCode {
72
12289
    match err {
73
        EngineError::Compile(_) => ErrorCode::new(ErrorCode::COMPILE),
74
        EngineError::OutOfFuel | EngineError::FiberLimit | EngineError::Trap(_) => {
75
338
            ErrorCode::new(ErrorCode::RUNTIME)
76
        }
77
325
        EngineError::EpochInterrupt => ErrorCode::new(ErrorCode::INTERRUPTED),
78
        EngineError::Instantiate(_) | EngineError::MissingExport(_) => {
79
            ErrorCode::new(ErrorCode::SERVER)
80
        }
81
        EngineError::Fuel(_) | EngineError::Config(_) | EngineError::CachePoisoned => {
82
            ErrorCode::new(ErrorCode::SERVER)
83
        }
84
        // Commodity mismatch now arrives as a `ScriptRaised` carrying the
85
        // reader-folded symbol `COMMODITY-MISMATCH` (ADR-0026): it `throw`s
86
        // `$nomi_error` in-guest and the boundary wrapper bridges the uncaught
87
        // throw to `__nomi_raise`. Both codes flow through verbatim: a script
88
        // raise carries the upper-cased symbol, a host condition its own
89
        // lowercase code.
90
        EngineError::ScriptRaised { code, .. } | EngineError::HostCondition { code, .. } => {
91
11626
            ErrorCode::new(code.clone())
92
        }
93
    }
94
12289
}