diff --git a/packages/hurl/src/output/raw.rs b/packages/hurl/src/output/raw.rs index 0d49c4d4263..b6693e570a0 100644 --- a/packages/hurl/src/output/raw.rs +++ b/packages/hurl/src/output/raw.rs @@ -18,7 +18,7 @@ use hurl_core::ast::SourceInfo; use hurl_core::error::Error; -use crate::runner::{HurlResult, RunnerError}; +use crate::runner::HurlResult; use crate::util::logger::Logger; use crate::{output, runner}; @@ -54,11 +54,8 @@ pub fn write_body( // FIXME: we convert to a runner::Error to be able to use fixme // method. Can we do otherwise (without creating an artificial // error a first character). - let error = runner::Error { - source_info: SourceInfo::new(0, 0, 0, 0), - inner: RunnerError::from(e), - assert: false, - }; + let source_info = SourceInfo::new(0, 0, 0, 0); + let error = runner::Error::new(source_info, e.into(), false); let message = error.fixme(); return Err(output::error::Error::new(&message)); } diff --git a/packages/hurl/src/report/junit/mod.rs b/packages/hurl/src/report/junit/mod.rs index e2ee010049f..f7f7e815f8a 100644 --- a/packages/hurl/src/report/junit/mod.rs +++ b/packages/hurl/src/report/junit/mod.rs @@ -156,13 +156,13 @@ mod tests { calls: vec![], captures: vec![], asserts: vec![], - errors: vec![Error { - source_info: SourceInfo::new(2, 10, 2, 13), - inner: RunnerError::AssertStatus { + errors: vec![Error::new( + SourceInfo::new(2, 10, 2, 13), + RunnerError::AssertStatus { actual: "404".to_string(), }, - assert: true, - }], + true, + )], time_in_ms: 0, compressed: false, }], @@ -180,13 +180,11 @@ mod tests { calls: vec![], captures: vec![], asserts: vec![], - errors: vec![Error { - source_info: SourceInfo::new(1, 5, 1, 19), - inner: RunnerError::HttpConnection( - "(6) Could not resolve host: unknown".to_string(), - ), - assert: false, - }], + errors: vec![Error::new( + SourceInfo::new(1, 5, 1, 19), + RunnerError::HttpConnection("(6) Could not resolve host: unknown".to_string()), + false, + )], time_in_ms: 0, compressed: false, }], diff --git a/packages/hurl/src/report/junit/testcase.rs b/packages/hurl/src/report/junit/testcase.rs index d4331c7b619..ea8ad53d5e8 100644 --- a/packages/hurl/src/report/junit/testcase.rs +++ b/packages/hurl/src/report/junit/testcase.rs @@ -122,13 +122,13 @@ HTTP/1.0 200 calls: vec![], captures: vec![], asserts: vec![], - errors: vec![Error { - source_info: SourceInfo::new(2, 10, 2, 13), - inner: RunnerError::AssertStatus { + errors: vec![Error::new( + SourceInfo::new(2, 10, 2, 13), + RunnerError::AssertStatus { actual: "404".to_string(), }, - assert: true, - }], + true, + )], time_in_ms: 0, compressed: false, }], @@ -161,13 +161,11 @@ HTTP/1.0 200 calls: vec![], captures: vec![], asserts: vec![], - errors: vec![Error { - source_info: SourceInfo::new(1, 5, 1, 19), - inner: RunnerError::HttpConnection( - "(6) Could not resolve host: unknown".to_string(), - ), - assert: false, - }], + errors: vec![Error::new( + SourceInfo::new(1, 5, 1, 19), + RunnerError::HttpConnection("(6) Could not resolve host: unknown".to_string()), + false, + )], time_in_ms: 0, compressed: false, }], diff --git a/packages/hurl/src/runner/entry.rs b/packages/hurl/src/runner/entry.rs index 7606d5078a5..2310800951a 100644 --- a/packages/hurl/src/runner/entry.rs +++ b/packages/hurl/src/runner/entry.rs @@ -22,7 +22,7 @@ use hurl_core::ast::*; use crate::http; use crate::http::ClientOptions; -use crate::runner::error::{Error, RunnerError}; +use crate::runner::error::Error; use crate::runner::request::{cookie_storage_clear, cookie_storage_set, eval_request}; use crate::runner::response::{eval_asserts, eval_captures, eval_version_status_asserts}; use crate::runner::result::{AssertResult, EntryResult}; @@ -95,8 +95,7 @@ pub fn run( start: entry.request.url.source_info.start, end: entry.request.url.source_info.end, }; - let inner = RunnerError::from(http_error); - let error = Error::new(source_info, inner, false); + let error = Error::new(source_info, http_error.into(), false); return EntryResult { entry_index, calls: vec![], diff --git a/packages/hurl/src/runner/filter/html_escape.rs b/packages/hurl/src/runner/filter/html_escape.rs index f1064fd03ba..6850e79355c 100644 --- a/packages/hurl/src/runner/filter/html_escape.rs +++ b/packages/hurl/src/runner/filter/html_escape.rs @@ -31,11 +31,10 @@ pub fn eval_html_escape( let encoded = html::html_escape(value); Ok(Some(Value::String(encoded))) } - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v._type()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v._type()); + Err(Error::new(source_info, inner, assert)) + } } } diff --git a/packages/hurl/src/runner/filter/html_unescape.rs b/packages/hurl/src/runner/filter/html_unescape.rs index 805b19df49e..52f92a34af5 100644 --- a/packages/hurl/src/runner/filter/html_unescape.rs +++ b/packages/hurl/src/runner/filter/html_unescape.rs @@ -31,11 +31,10 @@ pub fn eval_html_unescape( let decoded = html::html_unescape(value); Ok(Some(Value::String(decoded))) } - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v._type()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v._type()); + Err(Error::new(source_info, inner, assert)) + } } } diff --git a/packages/hurl/src/runner/filter/jsonpath.rs b/packages/hurl/src/runner/filter/jsonpath.rs index 6ff1e02c6d6..28a910ece4b 100644 --- a/packages/hurl/src/runner/filter/jsonpath.rs +++ b/packages/hurl/src/runner/filter/jsonpath.rs @@ -32,11 +32,10 @@ pub fn eval_jsonpath( ) -> Result, Error> { match value { Value::String(json) => eval_jsonpath_string(json, expr, variables, source_info), - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v._type()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v._type()); + Err(Error::new(source_info, inner, assert)) + } } } @@ -51,20 +50,17 @@ pub fn eval_jsonpath_string( let jsonpath_query = match jsonpath::parse(value.as_str()) { Ok(q) => q, Err(_) => { - return Err(Error { - source_info: *expr_source_info, - inner: RunnerError::QueryInvalidJsonpathExpression { value }, - assert: false, - }); + let inner = RunnerError::QueryInvalidJsonpathExpression { value }; + return Err(Error::new(*expr_source_info, inner, false)); } }; let value = match serde_json::from_str(json) { Err(_) => { - return Err(Error { + return Err(Error::new( source_info, - inner: RunnerError::QueryInvalidJson, - assert: false, - }); + RunnerError::QueryInvalidJson, + false, + )); } Ok(v) => v, }; diff --git a/packages/hurl/src/runner/filter/nth.rs b/packages/hurl/src/runner/filter/nth.rs index fbb439c11ff..109efbec2dc 100644 --- a/packages/hurl/src/runner/filter/nth.rs +++ b/packages/hurl/src/runner/filter/nth.rs @@ -28,21 +28,19 @@ pub fn eval_nth( ) -> Result, Error> { match value { Value::List(values) => match values.get(n as usize) { - None => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(format!( + None => { + let inner = RunnerError::FilterInvalidInput(format!( "Out of bound - size is {}", values.len() - )), - assert, - }), + )); + Err(Error::new(source_info, inner, assert)) + } Some(value) => Ok(Some(value.clone())), }, - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v.display()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v.display()); + Err(Error::new(source_info, inner, assert)) + } } } @@ -95,11 +93,11 @@ pub mod tests { ) .err() .unwrap(), - Error { - source_info: SourceInfo::new(1, 1, 1, 1), - inner: RunnerError::FilterInvalidInput("Out of bound - size is 2".to_string()), - assert: false - } + Error::new( + SourceInfo::new(1, 1, 1, 1), + RunnerError::FilterInvalidInput("Out of bound - size is 2".to_string()), + false + ) ); } } diff --git a/packages/hurl/src/runner/filter/regex.rs b/packages/hurl/src/runner/filter/regex.rs index 9c065f249fa..7f982b85c3b 100644 --- a/packages/hurl/src/runner/filter/regex.rs +++ b/packages/hurl/src/runner/filter/regex.rs @@ -39,11 +39,10 @@ pub fn eval_regex( }, None => Ok(None), }, - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v._type()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v._type()); + Err(Error::new(source_info, inner, assert)) + } } } diff --git a/packages/hurl/src/runner/filter/replace.rs b/packages/hurl/src/runner/filter/replace.rs index 8e54fc59647..46edba3b735 100644 --- a/packages/hurl/src/runner/filter/replace.rs +++ b/packages/hurl/src/runner/filter/replace.rs @@ -38,11 +38,10 @@ pub fn eval_replace( let s = re.replace_all(v, new_value).to_string(); Ok(Some(Value::String(s))) } - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v.display()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v.display()); + Err(Error::new(source_info, inner, assert)) + } } } diff --git a/packages/hurl/src/runner/filter/split.rs b/packages/hurl/src/runner/filter/split.rs index 351b4ba26f9..c495bd74044 100644 --- a/packages/hurl/src/runner/filter/split.rs +++ b/packages/hurl/src/runner/filter/split.rs @@ -38,11 +38,10 @@ pub fn eval_split( .collect(); Ok(Some(Value::List(values))) } - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v.display()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v.display()); + Err(Error::new(source_info, inner, assert)) + } } } diff --git a/packages/hurl/src/runner/filter/to_date.rs b/packages/hurl/src/runner/filter/to_date.rs index 4e456ef5c16..8f00f0ab789 100644 --- a/packages/hurl/src/runner/filter/to_date.rs +++ b/packages/hurl/src/runner/filter/to_date.rs @@ -38,17 +38,15 @@ pub fn eval_to_date( Ok(v) => Ok(Some(Value::Date( v.and_local_timezone(chrono::Utc).unwrap(), ))), - Err(_) => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(value.display()), - assert, - }), + Err(_) => { + let inner = RunnerError::FilterInvalidInput(value.display()); + Err(Error::new(source_info, inner, assert)) + } }, - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v.display()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v.display()); + Err(Error::new(source_info, inner, assert)) + } } } diff --git a/packages/hurl/src/runner/filter/to_int.rs b/packages/hurl/src/runner/filter/to_int.rs index 99f5d988f84..a16b4cedc11 100644 --- a/packages/hurl/src/runner/filter/to_int.rs +++ b/packages/hurl/src/runner/filter/to_int.rs @@ -30,17 +30,15 @@ pub fn eval_to_int( Value::Number(Number::Float(v)) => Ok(Some(Value::Number(Number::Integer(*v as i64)))), Value::String(v) => match v.parse::() { Ok(i) => Ok(Some(Value::Number(Number::Integer(i)))), - _ => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(value.display()), - assert, - }), + _ => { + let inner = RunnerError::FilterInvalidInput(value.display()); + Err(Error::new(source_info, inner, assert)) + } }, - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v.display()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v.display()); + Err(Error::new(source_info, inner, assert)) + } } } diff --git a/packages/hurl/src/runner/filter/url_decode.rs b/packages/hurl/src/runner/filter/url_decode.rs index 4cdf857988c..bfc2bcd59d8 100644 --- a/packages/hurl/src/runner/filter/url_decode.rs +++ b/packages/hurl/src/runner/filter/url_decode.rs @@ -29,18 +29,16 @@ pub fn eval_url_decode( Value::String(value) => { match percent_encoding::percent_decode(value.as_bytes()).decode_utf8() { Ok(decoded) => Ok(Some(Value::String(decoded.to_string()))), - Err(_) => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput("Invalid UTF8 stream".to_string()), - assert, - }), + Err(_) => { + let inner = RunnerError::FilterInvalidInput("Invalid UTF-8 stream".to_string()); + Err(Error::new(source_info, inner, assert)) + } } } - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v._type()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v._type()); + Err(Error::new(source_info, inner, assert)) + } } } diff --git a/packages/hurl/src/runner/filter/url_encode.rs b/packages/hurl/src/runner/filter/url_encode.rs index c3821da220a..54a1c4979cb 100644 --- a/packages/hurl/src/runner/filter/url_encode.rs +++ b/packages/hurl/src/runner/filter/url_encode.rs @@ -39,11 +39,10 @@ pub fn eval_url_encode( let encoded = percent_encoding::percent_encode(value.as_bytes(), FRAGMENT).to_string(); Ok(Some(Value::String(encoded))) } - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v._type()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v._type()); + Err(Error::new(source_info, inner, assert)) + } } } diff --git a/packages/hurl/src/runner/filter/xpath.rs b/packages/hurl/src/runner/filter/xpath.rs index 4244ae2051b..c0166b34250 100644 --- a/packages/hurl/src/runner/filter/xpath.rs +++ b/packages/hurl/src/runner/filter/xpath.rs @@ -35,11 +35,10 @@ pub fn eval_xpath( let is_html = true; eval_xpath_string(xml, expr, variables, source_info, is_html) } - v => Err(Error { - source_info, - inner: RunnerError::FilterInvalidInput(v._type()), - assert, - }), + v => { + let inner = RunnerError::FilterInvalidInput(v._type()); + Err(Error::new(source_info, inner, assert)) + } } } @@ -58,21 +57,17 @@ pub fn eval_xpath_string( }; match result { Ok(value) => Ok(Some(value)), - Err(xpath::XpathError::InvalidXml) => Err(Error { - source_info, - inner: RunnerError::QueryInvalidXml, - assert: false, - }), - Err(xpath::XpathError::InvalidHtml) => Err(Error { - source_info, - inner: RunnerError::QueryInvalidXml, - assert: false, - }), - Err(xpath::XpathError::Eval) => Err(Error { - source_info: expr_template.source_info, - inner: RunnerError::QueryInvalidXpathEval, - assert: false, - }), + Err(xpath::XpathError::InvalidXml) => { + Err(Error::new(source_info, RunnerError::QueryInvalidXml, false)) + } + Err(xpath::XpathError::InvalidHtml) => { + Err(Error::new(source_info, RunnerError::QueryInvalidXml, false)) + } + Err(xpath::XpathError::Eval) => Err(Error::new( + expr_template.source_info, + RunnerError::QueryInvalidXpathEval, + false, + )), Err(xpath::XpathError::Unsupported) => { panic!("Unsupported xpath {expr}"); // good usecase for panic - I could not reproduce this usecase myself } diff --git a/packages/hurl/src/runner/json.rs b/packages/hurl/src/runner/json.rs index c6c1a18f395..19bdb41bc2d 100644 --- a/packages/hurl/src/runner/json.rs +++ b/packages/hurl/src/runner/json.rs @@ -82,11 +82,8 @@ pub fn eval_json_value( if parse_json_null(&mut reader).is_ok() { return Ok(s); } - Err(Error { - source_info: exp.variable.source_info, - inner: RunnerError::InvalidJson { value: s }, - assert: false, - }) + let inner = RunnerError::InvalidJson { value: s }; + Err(Error::new(exp.variable.source_info, inner, false)) } } } diff --git a/packages/hurl/src/runner/options.rs b/packages/hurl/src/runner/options.rs index ee7a491fd82..5c3ccd9cdea 100644 --- a/packages/hurl/src/runner/options.rs +++ b/packages/hurl/src/runner/options.rs @@ -257,15 +257,14 @@ fn eval_boolean_option( BooleanOption::Literal(value) => Ok(*value), BooleanOption::Expression(expr) => match eval_expression(expr, variables)? { Value::Bool(value) => Ok(value), - v => Err(Error { - source_info: expr.variable.source_info, - inner: RunnerError::TemplateVariableInvalidType { + v => { + let inner = RunnerError::TemplateVariableInvalidType { name: expr.variable.name.clone(), value: v.to_string(), expecting: "boolean".to_string(), - }, - assert: false, - }), + }; + Err(Error::new(expr.variable.source_info, inner, false)) + } }, } } @@ -279,28 +278,24 @@ fn eval_natural_option( NaturalOption::Expression(expr) => match eval_expression(expr, variables)? { Value::Number(Number::Integer(value)) => { if value < 0 { - Err(Error { - source_info: expr.variable.source_info, - inner: RunnerError::TemplateVariableInvalidType { - name: expr.variable.name.clone(), - value: value.to_string(), - expecting: "positive integer".to_string(), - }, - assert: false, - }) + let inner = RunnerError::TemplateVariableInvalidType { + name: expr.variable.name.clone(), + value: value.to_string(), + expecting: "positive integer".to_string(), + }; + Err(Error::new(expr.variable.source_info, inner, false)) } else { Ok(value as u64) } } - v => Err(Error { - source_info: expr.variable.source_info, - inner: RunnerError::TemplateVariableInvalidType { + v => { + let inner = RunnerError::TemplateVariableInvalidType { name: expr.variable.name.clone(), value: v.to_string(), expecting: "positive integer".to_string(), - }, - assert: false, - }), + }; + Err(Error::new(expr.variable.source_info, inner, false)) + } }, } } @@ -320,26 +315,22 @@ fn eval_retry_option( } else if value > 0 { Ok(Retry::Finite(value as usize)) } else { - Err(Error { - source_info: expr.variable.source_info, - inner: RunnerError::TemplateVariableInvalidType { - name: expr.variable.name.clone(), - value: value.to_string(), - expecting: "integer".to_string(), - }, - assert: false, - }) + let inner = RunnerError::TemplateVariableInvalidType { + name: expr.variable.name.clone(), + value: value.to_string(), + expecting: "integer".to_string(), + }; + Err(Error::new(expr.variable.source_info, inner, false)) } } - v => Err(Error { - source_info: expr.variable.source_info, - inner: RunnerError::TemplateVariableInvalidType { + v => { + let inner = RunnerError::TemplateVariableInvalidType { name: expr.variable.name.clone(), value: v.to_string(), expecting: "integer".to_string(), - }, - assert: false, - }), + }; + Err(Error::new(expr.variable.source_info, inner, false)) + } }, } } diff --git a/packages/hurl/src/runner/predicate.rs b/packages/hurl/src/runner/predicate.rs index abd83ce67cc..925220076f4 100644 --- a/packages/hurl/src/runner/predicate.rs +++ b/packages/hurl/src/runner/predicate.rs @@ -63,35 +63,26 @@ pub fn eval_predicate( if assert_result.type_mismatch { let not = if predicate.not { "not " } else { "" }; let expected = format!("{}{}", not, assert_result.expected); - Err(Error { - source_info, - inner: RunnerError::AssertFailure { - actual: assert_result.actual, - expected, - type_mismatch: true, - }, - assert: true, - }) + let inner = RunnerError::AssertFailure { + actual: assert_result.actual, + expected, + type_mismatch: true, + }; + Err(Error::new(source_info, inner, true)) } else if predicate.not && assert_result.success { - Err(Error { - source_info, - inner: RunnerError::AssertFailure { - actual: assert_result.actual, - expected: format!("not {}", assert_result.expected), - type_mismatch: false, - }, - assert: true, - }) + let inner = RunnerError::AssertFailure { + actual: assert_result.actual, + expected: format!("not {}", assert_result.expected), + type_mismatch: false, + }; + Err(Error::new(source_info, inner, true)) } else if !predicate.not && !assert_result.success { - Err(Error { - source_info, - inner: RunnerError::AssertFailure { - actual: assert_result.actual, - expected: assert_result.expected, - type_mismatch: false, - }, - assert: true, - }) + let inner = RunnerError::AssertFailure { + actual: assert_result.actual, + expected: assert_result.expected, + type_mismatch: false, + }; + Err(Error::new(source_info, inner, true)) } else { Ok(()) } @@ -487,13 +478,7 @@ fn eval_match( let expected = eval_template(template, variables)?; match regex::Regex::new(expected.as_str()) { Ok(re) => re, - Err(_) => { - return Err(Error { - source_info, - inner: RunnerError::InvalidRegex, - assert: false, - }); - } + Err(_) => return Err(Error::new(source_info, RunnerError::InvalidRegex, false)), } } PredicateValue::Regex(regex) => regex.inner.clone(), diff --git a/packages/hurl/src/runner/query.rs b/packages/hurl/src/runner/query.rs index a2a44085032..6cdb8c893d4 100644 --- a/packages/hurl/src/runner/query.rs +++ b/packages/hurl/src/runner/query.rs @@ -119,11 +119,7 @@ fn eval_query_body(response: &http::Response, query_source_info: SourceInfo) -> // Can return a string if encoding is known and utf8. match response.text() { Ok(s) => Ok(Some(Value::String(s))), - Err(inner) => Err(Error { - source_info: query_source_info, - inner: RunnerError::from(inner), - assert: false, - }), + Err(inner) => Err(Error::new(query_source_info, inner.into(), false)), } } @@ -131,17 +127,13 @@ fn eval_query_xpath( response: &http::Response, expr: &Template, variables: &HashMap, - source_info: SourceInfo, + query_source_info: SourceInfo, ) -> QueryResult { match response.text() { - Err(inner) => Err(Error { - source_info, - inner: RunnerError::from(inner), - assert: false, - }), Ok(xml) => { - filter::eval_xpath_string(&xml, expr, variables, source_info, response.is_html()) + filter::eval_xpath_string(&xml, expr, variables, query_source_info, response.is_html()) } + Err(inner) => Err(Error::new(query_source_info, inner.into(), false)), } } @@ -152,12 +144,8 @@ fn eval_query_jsonpath( query_source_info: SourceInfo, ) -> QueryResult { match response.text() { - Err(inner) => Err(Error { - source_info: query_source_info, - inner: RunnerError::from(inner), - assert: false, - }), Ok(json) => filter::eval_jsonpath_string(&json, expr, variables, query_source_info), + Err(inner) => Err(Error::new(query_source_info, inner.into(), false)), } } @@ -168,28 +156,15 @@ fn eval_query_regex( query_source_info: SourceInfo, ) -> QueryResult { let s = match response.text() { - Err(inner) => { - return Err(Error { - source_info: query_source_info, - inner: RunnerError::from(inner), - assert: false, - }); - } Ok(v) => v, + Err(inner) => return Err(Error::new(query_source_info, inner.into(), false)), }; let re = match regex { RegexValue::Template(t) => { let value = eval_template(t, variables)?; match Regex::new(value.as_str()) { Ok(re) => re, - Err(_) => { - let source_info = t.source_info; - return Err(Error { - source_info, - inner: RunnerError::InvalidRegex, - assert: false, - }); - } + Err(_) => return Err(Error::new(t.source_info, RunnerError::InvalidRegex, false)), } } RegexValue::Regex(re) => re.inner.clone(), @@ -221,11 +196,7 @@ fn eval_query_duration(response: &http::Response) -> QueryResult { fn eval_query_bytes(response: &http::Response, query_source_info: SourceInfo) -> QueryResult { match response.uncompress_body() { Ok(s) => Ok(Some(Value::Bytes(s))), - Err(inner) => Err(Error { - source_info: query_source_info, - inner: RunnerError::from(inner), - assert: false, - }), + Err(inner) => Err(Error::new(query_source_info, inner.into(), false)), } } @@ -233,11 +204,7 @@ fn eval_query_sha256(response: &http::Response, query_source_info: SourceInfo) - let bytes = match response.uncompress_body() { Ok(s) => s, Err(inner) => { - return Err(Error { - source_info: query_source_info, - inner: RunnerError::from(inner), - assert: false, - }) + return Err(Error::new(query_source_info, inner.into(), false)); } }; let mut hasher = sha2::Sha256::new(); @@ -251,11 +218,7 @@ fn eval_query_md5(response: &http::Response, query_source_info: SourceInfo) -> Q let bytes = match response.uncompress_body() { Ok(s) => s, Err(inner) => { - return Err(Error { - source_info: query_source_info, - inner: RunnerError::from(inner), - assert: false, - }) + return Err(Error::new(query_source_info, inner.into(), false)); } }; let bytes = md5::compute(bytes).to_vec(); diff --git a/packages/hurl/src/runner/regex.rs b/packages/hurl/src/runner/regex.rs index dbad5d97940..89943b55935 100644 --- a/packages/hurl/src/runner/regex.rs +++ b/packages/hurl/src/runner/regex.rs @@ -32,11 +32,7 @@ pub fn eval_regex_value( let value = eval_template(t, variables)?; match Regex::new(value.as_str()) { Ok(re) => Ok(re), - Err(_) => Err(Error { - source_info: t.source_info, - inner: RunnerError::InvalidRegex, - assert: false, - }), + Err(_) => Err(Error::new(t.source_info, RunnerError::InvalidRegex, false)), } } RegexValue::Regex(re) => Ok(re.inner.clone()), diff --git a/packages/hurl/src/runner/response.rs b/packages/hurl/src/runner/response.rs index f9830ad59e7..0900100dbec 100644 --- a/packages/hurl/src/runner/response.rs +++ b/packages/hurl/src/runner/response.rs @@ -72,33 +72,36 @@ pub fn eval_asserts( for header in response.headers.iter() { match eval_template(&header.value, variables) { Err(e) => { - asserts.push(AssertResult::Header { + let result = AssertResult::Header { actual: Err(e), expected: String::new(), - source_info: header.key.clone().source_info, - }); + source_info: header.key.source_info, + }; + asserts.push(result); } Ok(expected) => { match eval_template(&header.key, variables) { Ok(header_name) => { let actuals = http_response.get_header_values(&header_name); if actuals.is_empty() { - asserts.push(AssertResult::Header { - actual: Err(Error { - source_info: header.key.source_info, - inner: RunnerError::QueryHeaderNotFound, - assert: false, - }), + let result = AssertResult::Header { + actual: Err(Error::new( + header.key.source_info, + RunnerError::QueryHeaderNotFound, + false, + )), expected, source_info: header.key.source_info, - }); + }; + asserts.push(result); } else if actuals.len() == 1 { let actual = actuals.first().unwrap().to_string(); - asserts.push(AssertResult::Header { + let result = AssertResult::Header { actual: Ok(actual), expected, - source_info: header.value.clone().source_info, - }); + source_info: header.value.source_info, + }; + asserts.push(result); } else { // failure by default // expected value not found in the list @@ -117,18 +120,19 @@ pub fn eval_asserts( break; } } - asserts.push(AssertResult::Header { + let result = AssertResult::Header { actual: Ok(actual), expected, - source_info: header.value.clone().source_info, - }); + source_info: header.value.source_info, + }; + asserts.push(result); } } Err(e) => { asserts.push(AssertResult::Header { actual: Err(e), expected, - source_info: header.value.clone().source_info, + source_info: header.value.source_info, }); } } @@ -163,14 +167,13 @@ fn eval_implicit_body_asserts( }; let actual = match http_response.text() { Ok(s) => Ok(Value::String(s)), - Err(e) => Err(Error { - source_info: SourceInfo { + Err(e) => { + let source_info = SourceInfo { start: spec_body.space0.source_info.end, end: spec_body.space0.source_info.end, - }, - inner: RunnerError::from(e), - assert: true, - }), + }; + Err(Error::new(source_info, e.into(), true)) + } }; AssertResult::Body { actual, @@ -182,14 +185,13 @@ fn eval_implicit_body_asserts( let expected = Ok(Value::String(value.to_string())); let actual = match http_response.text() { Ok(s) => Ok(Value::String(s)), - Err(e) => Err(Error { - source_info: SourceInfo { + Err(e) => { + let source_info = SourceInfo { start: spec_body.space0.source_info.end, end: spec_body.space0.source_info.end, - }, - inner: RunnerError::from(e), - assert: true, - }), + }; + Err(Error::new(source_info, e.into(), true)) + } }; AssertResult::Body { actual, @@ -204,14 +206,13 @@ fn eval_implicit_body_asserts( }; let actual = match http_response.text() { Ok(s) => Ok(Value::String(s)), - Err(e) => Err(Error { - source_info: SourceInfo { + Err(e) => { + let source_info = SourceInfo { start: spec_body.space0.source_info.end, end: spec_body.space0.source_info.end, - }, - inner: RunnerError::from(e), - assert: true, - }), + }; + Err(Error::new(source_info, e.into(), true)) + } }; AssertResult::Body { actual, @@ -226,14 +227,13 @@ fn eval_implicit_body_asserts( }; let actual = match http_response.text() { Ok(s) => Ok(Value::String(s)), - Err(e) => Err(Error { - source_info: SourceInfo { + Err(e) => { + let source_info = SourceInfo { start: spec_body.space0.source_info.end, end: spec_body.space0.source_info.end, - }, - inner: RunnerError::from(e), - assert: true, - }), + }; + Err(Error::new(source_info, e.into(), true)) + } }; AssertResult::Body { actual, @@ -250,14 +250,13 @@ fn eval_implicit_body_asserts( let expected = Ok(Value::Bytes(value.to_vec())); let actual = match http_response.uncompress_body() { Ok(b) => Ok(Value::Bytes(b)), - Err(e) => Err(Error { - source_info: SourceInfo { + Err(e) => { + let source_info = SourceInfo { start: spec_body.space0.source_info.end, end: spec_body.space0.source_info.end, - }, - inner: RunnerError::from(e), - assert: true, - }), + }; + Err(Error::new(source_info, e.into(), true)) + } }; AssertResult::Body { actual, @@ -277,14 +276,13 @@ fn eval_implicit_body_asserts( let expected = Ok(Value::Bytes(value.to_vec())); let actual = match http_response.uncompress_body() { Ok(b) => Ok(Value::Bytes(b)), - Err(e) => Err(Error { - source_info: SourceInfo { + Err(e) => { + let source_info = SourceInfo { start: spec_body.space0.source_info.end, end: spec_body.space0.source_info.end, - }, - inner: RunnerError::from(e), - assert: true, - }), + }; + Err(Error::new(source_info, e.into(), true)) + } }; AssertResult::Body { actual, @@ -302,14 +300,13 @@ fn eval_implicit_body_asserts( }; let actual = match http_response.uncompress_body() { Ok(b) => Ok(Value::Bytes(b)), - Err(e) => Err(Error { - source_info: SourceInfo { + Err(e) => { + let source_info = SourceInfo { start: spec_body.space0.source_info.end, end: spec_body.space0.source_info.end, - }, - inner: RunnerError::from(e), - assert: true, - }), + }; + Err(Error::new(source_info, e.into(), true)) + } }; AssertResult::Body { actual, @@ -403,15 +400,15 @@ mod tests { vec![AssertResult::Explicit { actual: Ok(Some(Value::Number(Number::Integer(2)))), source_info: SourceInfo::new(1, 22, 1, 24), - predicate_result: Some(Err(Error { - source_info: SourceInfo::new(1, 0, 1, 0), - inner: RunnerError::AssertFailure { + predicate_result: Some(Err(Error::new( + SourceInfo::new(1, 0, 1, 0), + RunnerError::AssertFailure { actual: "int <2>".to_string(), expected: "int <3>".to_string(), type_mismatch: false, }, - assert: true, - })), + true + ))), },] ); } diff --git a/packages/hurl/src/runner/template.rs b/packages/hurl/src/runner/template.rs index 9f5640176ca..ff93a17e0e1 100644 --- a/packages/hurl/src/runner/template.rs +++ b/packages/hurl/src/runner/template.rs @@ -55,14 +55,11 @@ pub fn render_expression(expr: &Expr, variables: &HashMap) -> Res if value.is_renderable() { Ok(value.clone().to_string()) } else { - Err(Error { - source_info, - inner: RunnerError::UnrenderableVariable { - name: name.to_string(), - value: value.to_string(), - }, - assert: false, - }) + let inner = RunnerError::UnrenderableVariable { + name: name.to_string(), + value: value.to_string(), + }; + Err(Error::new(source_info, inner, false)) } } @@ -71,11 +68,10 @@ pub fn eval_expression(expr: &Expr, variables: &HashMap) -> Resul let name = &expr.variable.name; match variables.get(name.as_str()) { Some(value) => Ok(value.clone()), - _ => Err(Error { - source_info, - inner: RunnerError::TemplateVariableNotDefined { name: name.clone() }, - assert: false, - }), + _ => { + let inner = RunnerError::TemplateVariableNotDefined { name: name.clone() }; + Err(Error::new(source_info, inner, false)) + } } } diff --git a/packages/hurl/src/util/logger.rs b/packages/hurl/src/util/logger.rs index 5ea18f61d8e..a42d6ea75a8 100644 --- a/packages/hurl/src/util/logger.rs +++ b/packages/hurl/src/util/logger.rs @@ -698,13 +698,10 @@ pub mod tests { HTTP/1.0 200 "#; let filename = "test.hurl"; - let error = runner::Error { - source_info: SourceInfo::new(2, 10, 2, 13), - inner: runner::RunnerError::AssertStatus { - actual: "404".to_string(), - }, - assert: true, + let inner = runner::RunnerError::AssertStatus { + actual: "404".to_string(), }; + let error = runner::Error::new(SourceInfo::new(2, 10, 2, 13), inner, true); assert_eq!( error_string(filename, content, &error, false), r#"Assert status code @@ -724,11 +721,11 @@ HTTP/1.0 200 xpath "strong(//head/title)" == "Hello" "#; let filename = "test.hurl"; - let error = runner::Error { - source_info: SourceInfo::new(4, 7, 4, 29), - inner: runner::RunnerError::QueryInvalidXpathEval, - assert: true, - }; + let error = runner::Error::new( + SourceInfo::new(4, 7, 4, 29), + runner::RunnerError::QueryInvalidXpathEval, + true, + ); assert_eq!( error_string(filename, content, &error, false), r#"Invalid XPath expression @@ -777,14 +774,11 @@ HTTP/1.0 200 ``` "#; let filename = "test.hurl"; - let error = runner::Error { - source_info: SourceInfo::new(3, 4, 4, 1), - inner: runner::RunnerError::AssertBodyValueError { - actual: "

Hello

\n\n".to_string(), - expected: "

Hello

\n".to_string(), - }, - assert: true, + let inner = runner::RunnerError::AssertBodyValueError { + actual: "

Hello

\n\n".to_string(), + expected: "

Hello

\n".to_string(), }; + let error = runner::Error::new(SourceInfo::new(3, 4, 4, 1), inner, true); assert_eq!( error_string(filename, content, &error, false), r#"Assert body value