Lines
97.96 %
Functions
96.15 %
Branches
100 %
//! Structured condition detail (EXD): `(error 'code "msg" detail)` end to end through
//! the `Session`, the wire frame and `catch-each`.
use super::*;
use crate::envelope::format_response;
async fn frame(source: &str) -> String {
let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
let outcome = session.handle_request(&format!("(progn {source})")).await;
format_response(&Response {
stream: 0,
id: RequestId::Int(1),
payload: outcome.payload,
})
}
#[tokio::test]
async fn an_uncaught_raise_with_a_plist_detail_puts_it_on_the_wire() {
assert_eq!(
frame(r#"(error 'boom "bad input" (list :field "a" :count 3))"#).await,
concat!(
r#"(:id 1 :error (:code BOOM :message "script raised BOOM: bad input" "#,
r#":detail (:FIELD "a" :COUNT 3)))"#
)
);
async fn an_uncaught_raise_without_detail_has_no_detail_on_the_wire() {
frame(r#"(error 'boom "bad input")"#).await,
r#"(:id 1 :error (:code BOOM :message "script raised BOOM: bad input"))"#
async fn a_scalar_detail_is_printed_as_its_value() {
let wire = frame(r#"(error 'boom "m" 42)"#).await;
assert!(wire.ends_with(" :detail 42))"), "{wire}");
async fn a_caught_detail_is_the_plist() {
let payload = frame(
r#"(handler-case (error 'boom "m" (list :field "a" :count 3))
(boom (e) (getf (error-detail e) :count)))"#,
.await;
assert_eq!(payload, "(:id 1 :value 3)");
async fn the_catch_each_err_cell_carries_code_and_message_only() {
let wire = frame(r#"(catch-each (list 1) x (error 'boom "bad" (list :n x)))"#).await;
assert_eq!(wire, r#"(:id 1 :value ((ERR BOOM "bad")))"#);
async fn a_four_argument_raise_is_an_arity_error() {
let wire = frame(r#"(error 'boom "m" nil nil)"#).await;
assert!(wire.contains(":code args"), "{wire}");
fn deep_detail_raise(levels: usize) -> String {
let bindings: String = (0..levels)
.map(|n| {
let inner = if n == 0 {
"1".to_string()
} else {
format!("v{}", n - 1)
};
format!("(v{n} (list {inner})) ")
.collect();
format!(
r#"(let* ({bindings}) (error 'deep "too nested" v{}))"#,
levels - 1
async fn a_detail_nested_past_the_depth_limit_prints_the_placeholder() {
let wire = frame(&deep_detail_raise(100)).await;
assert!(
wire.starts_with("(:id 1 :error (:code DEEP :message "),
"{wire}"
assert!(wire.contains("too nested"), "{wire}");
assert!(wire.contains("#<...>"), "{wire}");
assert!(!wire.contains("undecodable"), "{wire}");
async fn deep_detail_payload(levels: usize) -> ResponsePayload {
let wire = frame(&deep_detail_raise(levels)).await;
crate::parse_response(&wire)
.unwrap_or_else(|e| panic!("client cannot read {levels} levels: {e}\n{wire}"))
.payload
fn assert_code_and_message(payload: &ResponsePayload) {
matches!(payload, ResponsePayload::Error { code, message, .. }
if code.as_symbol() == "deep" && message.contains("too nested")),
"{payload:?}"
async fn a_detail_one_below_the_ceiling_is_read_back_structured() {
let payload = deep_detail_payload(scripting::MAX_WIRE_LIST_DEPTH - 1).await;
assert_code_and_message(&payload);
matches!(
&payload,
ResponsePayload::Error {
detail: Some(Value::Pair(_)),
..
),
async fn a_detail_at_the_ceiling_keeps_its_structure_and_prints_the_placeholder() {
let payload = deep_detail_payload(scripting::MAX_WIRE_LIST_DEPTH).await;
let ResponsePayload::Error {
detail: Some(detail),
} = &payload
else {
panic!("the raise carries a detail: {payload:?}");
assert!(matches!(detail, Value::Pair(_)), "{detail:?}");
let printed = nomiscript::format_value(detail);
assert!(printed.contains("#<...>"), "{printed}");
assert!(!printed.contains("undecodable"), "{printed}");