Lines
91.11 %
Functions
100 %
Branches
//! Mapping of engine, envelope and compiler errors to the wire error payload.
use nomiscript::{Error as NomiError, Value};
use scripting::runtime::EngineError;
use crate::envelope::{EnvelopeError, ErrorCode, ResponsePayload};
pub(super) fn engine_payload(err: EngineError) -> ResponsePayload {
ResponsePayload::Error {
code: engine_error_code(&err),
message: err.to_string(),
detail: engine_error_detail(err),
}
/// A native's thrown condition reaches the boundary as an upper-case `ScriptRaised`;
/// the wire keeps the lower-case host code and the plain message.
pub(crate) fn restore_host_condition(err: EngineError) -> EngineError {
match err {
EngineError::ScriptRaised {
code,
message,
detail,
} if ErrorCode::HOST_CONDITIONS.contains(&code.to_lowercase().as_str()) => {
EngineError::HostCondition {
code: code.to_lowercase(),
other => other,
pub(super) fn envelope_error_code(err: &EnvelopeError) -> ErrorCode {
let symbol = match err {
EnvelopeError::Parse(_)
| EnvelopeError::NotSingleExpr
| EnvelopeError::NotPlist
| EnvelopeError::InvalidStream(..)
| EnvelopeError::Unsupported => ErrorCode::PARSE,
EnvelopeError::MissingKey(_) | EnvelopeError::InvalidValue(_, _) => ErrorCode::ARGS,
};
ErrorCode::new(symbol)
pub(super) fn nomi_error_code(err: &NomiError) -> ErrorCode {
NomiError::Parse(_) => ErrorCode::PARSE,
NomiError::Compile(_)
| NomiError::Resource(_)
| NomiError::Dimension(_)
| NomiError::Transfer { .. }
| NomiError::UnclosableFactory { .. }
| NomiError::UndefinedSymbol(_) => ErrorCode::COMPILE,
NomiError::Runtime(_) => ErrorCode::RUNTIME,
NomiError::Type { .. } | NomiError::Arity { .. } => ErrorCode::ARGS,
fn engine_error_detail(err: EngineError) -> Option<Value> {
EngineError::HostCondition { detail, .. } | EngineError::ScriptRaised { detail, .. } => {
detail.map(Value::from)
other => Some(Value::String(format!("{other:?}"))),
pub(super) fn engine_error_code(err: &EngineError) -> ErrorCode {
EngineError::Compile(_) => ErrorCode::new(ErrorCode::COMPILE),
EngineError::OutOfFuel | EngineError::FiberLimit | EngineError::Trap(_) => {
ErrorCode::new(ErrorCode::RUNTIME)
EngineError::EpochInterrupt => ErrorCode::new(ErrorCode::INTERRUPTED),
EngineError::Instantiate(_) | EngineError::MissingExport(_) => {
ErrorCode::new(ErrorCode::SERVER)
EngineError::Fuel(_) | EngineError::Config(_) | EngineError::CachePoisoned => {
// Commodity mismatch now arrives as a `ScriptRaised` carrying the
// reader-folded symbol `COMMODITY-MISMATCH` (ADR-0026): it `throw`s
// `$nomi_error` in-guest and the boundary wrapper bridges the uncaught
// throw to `__nomi_raise`. Both codes flow through verbatim: a script
// raise carries the upper-cased symbol, a host condition its own
// lowercase code.
EngineError::ScriptRaised { code, .. } | EngineError::HostCondition { code, .. } => {
ErrorCode::new(code.clone())