From 6feacbeef513420eddd9cbaed36abea80344de13 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Tue, 12 May 2020 12:22:28 +1000 Subject: [PATCH] fix: throw an exception when a response is configured but no interaction defined --- native/src/lib.rs | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/native/src/lib.rs b/native/src/lib.rs index 313af2a33..31d131547 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -318,31 +318,39 @@ declare_types! { let mut this = cx.this(); - { + let result = { let guard = cx.lock(); let mut pact = this.borrow_mut(&guard); if let Some(last) = pact.interactions.last_mut() { - if let Ok(status) = js_status { - match status.downcast::() { - Ok(status) => last.response.status = status.value() as u16, - Err(err) => warn!("Response status is not a number - {}", err) - } - } - if let Ok(header_props) = js_header_props { - last.response.headers = Some(header_props) + if let Ok(status) = js_status { + match status.downcast::() { + Ok(status) => last.response.status = status.value() as u16, + Err(err) => warn!("Response status is not a number - {}", err) } + } + if let Ok(header_props) = js_header_props { + last.response.headers = Some(header_props) + } - if let Some(body) = js_body { - match process_body(body, last.response.content_type_enum(), &mut last.response.matching_rules, - &mut last.response.generators) { - Ok(body) => last.response.body = body, - Err(err) => panic!(err) - } + if let Some(body) = js_body { + match process_body(body, last.response.content_type_enum(), &mut last.response.matching_rules, + &mut last.response.generators) { + Ok(body) => last.response.body = body, + Err(err) => panic!(err) } + } + Ok(()) + } else if pact.interactions.is_empty() { + Err("You need to define a new interaction with the uponReceiving method before you can define a new response with the willRespondWith method") + } else { + Ok(()) } - } + }; - Ok(cx.undefined().upcast()) + match result { + Ok(_) => Ok(cx.undefined().upcast()), + Err(message) => cx.throw_error(message) + } } method executeTest(mut cx) {