From 5af040c515578f130ee260090ff436dde7e5fd54 Mon Sep 17 00:00:00 2001 From: jcamiel Date: Sun, 26 Nov 2023 18:03:06 +0100 Subject: [PATCH] Add new constructor method on runner error. --- packages/hurl/src/runner/assert.rs | 37 ++++++------------- packages/hurl/src/runner/body.rs | 18 +++------ packages/hurl/src/runner/capture.rs | 20 +++++----- packages/hurl/src/runner/entry.rs | 19 +++------- packages/hurl/src/runner/error.rs | 10 +++++ packages/hurl/src/runner/expr.rs | 11 ++---- packages/hurl/src/runner/filter/count.rs | 9 ++--- .../hurl/src/runner/filter/days_after_now.rs | 9 ++--- .../hurl/src/runner/filter/days_before_now.rs | 9 ++--- packages/hurl/src/runner/filter/decode.rs | 27 ++++++-------- packages/hurl/src/runner/filter/eval.rs | 10 ++--- packages/hurl/src/runner/filter/format.rs | 9 ++--- 12 files changed, 81 insertions(+), 107 deletions(-) diff --git a/packages/hurl/src/runner/assert.rs b/packages/hurl/src/runner/assert.rs index 7eed71eb9be..7818a01dd9b 100644 --- a/packages/hurl/src/runner/assert.rs +++ b/packages/hurl/src/runner/assert.rs @@ -42,13 +42,10 @@ impl AssertResult { { None } else { - Some(Error { - source_info: *source_info, - inner: RunnerError::AssertVersion { - actual: actual.clone(), - }, - assert: false, - }) + let inner = RunnerError::AssertVersion { + actual: actual.to_string(), + }; + Some(Error::new(*source_info, inner, false)) } } AssertResult::Status { @@ -59,13 +56,10 @@ impl AssertResult { if actual == expected { None } else { - Some(Error { - source_info: *source_info, - inner: RunnerError::AssertStatus { - actual: actual.to_string(), - }, - assert: false, - }) + let inner = RunnerError::AssertStatus { + actual: actual.to_string(), + }; + Some(Error::new(*source_info, inner, false)) } } AssertResult::Header { @@ -78,11 +72,8 @@ impl AssertResult { if s == expected { None } else { - Some(Error { - source_info: *source_info, - inner: RunnerError::AssertHeaderValueError { actual: s.clone() }, - assert: false, - }) + let inner = RunnerError::AssertHeaderValueError { actual: s.clone() }; + Some(Error::new(*source_info, inner, false)) } } }, @@ -100,16 +91,12 @@ impl AssertResult { } else { let actual = actual.to_string(); let expected = expected.to_string(); - Some(Error { - source_info: *source_info, - inner: RunnerError::AssertBodyValueError { actual, expected }, - assert: false, - }) + let inner = RunnerError::AssertBodyValueError { actual, expected }; + Some(Error::new(*source_info, inner, false)) } } }, }, - AssertResult::Explicit { actual: Err(e), .. } => Some(e.clone()), AssertResult::Explicit { predicate_result: Some(Err(e)), diff --git a/packages/hurl/src/runner/body.rs b/packages/hurl/src/runner/body.rs index a1b1b4d50f1..f0eba76b109 100644 --- a/packages/hurl/src/runner/body.rs +++ b/packages/hurl/src/runner/body.rs @@ -69,22 +69,16 @@ pub fn eval_file(filename: &Filename, context_dir: &ContextDir) -> Result Ok(value), - Err(_) => Err(Error { - source_info: filename.source_info, - inner: RunnerError::FileReadAccess { value: file }, - assert: false, - }), + Err(_) => Err(Error::new(filename.source_info, inner, false)), } } diff --git a/packages/hurl/src/runner/capture.rs b/packages/hurl/src/runner/capture.rs index 4cd43981a4a..e0e45ee903d 100644 --- a/packages/hurl/src/runner/capture.rs +++ b/packages/hurl/src/runner/capture.rs @@ -38,21 +38,21 @@ pub fn eval_capture( let value = eval_query(&capture.query, variables, http_response)?; let value = match value { None => { - return Err(Error { - source_info: capture.query.source_info, - inner: RunnerError::NoQueryResult, - assert: false, - }); + return Err(Error::new( + capture.query.source_info, + RunnerError::NoQueryResult, + false, + )); } Some(value) => { let filters = capture.filters.iter().map(|(_, f)| f.clone()).collect(); match eval_filters(&filters, &value, variables, false)? { None => { - return Err(Error { - source_info: capture.query.source_info, - inner: RunnerError::NoQueryResult, - assert: false, - }) + return Err(Error::new( + capture.query.source_info, + RunnerError::NoQueryResult, + false, + )); } Some(v) => v, } diff --git a/packages/hurl/src/runner/entry.rs b/packages/hurl/src/runner/entry.rs index f7710f35ec7..7606d5078a5 100644 --- a/packages/hurl/src/runner/entry.rs +++ b/packages/hurl/src/runner/entry.rs @@ -91,15 +91,12 @@ pub fn run( let calls = match http_client.execute_with_redirect(&http_request, &client_options, logger) { Ok(calls) => calls, Err(http_error) => { - let runner_error = RunnerError::from(http_error); - let error = Error { - source_info: SourceInfo { - start: entry.request.url.source_info.start, - end: entry.request.url.source_info.end, - }, - inner: runner_error, - assert: false, + let source_info = SourceInfo { + 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); return EntryResult { entry_index, calls: vec![], @@ -208,11 +205,7 @@ fn asserts_to_errors(asserts: &[AssertResult]) -> Vec { .map( |Error { source_info, inner, .. - }| Error { - source_info, - inner, - assert: true, - }, + }| Error::new(source_info, inner, true), ) .collect() } diff --git a/packages/hurl/src/runner/error.rs b/packages/hurl/src/runner/error.rs index 2973f82ec8e..ced59a46292 100644 --- a/packages/hurl/src/runner/error.rs +++ b/packages/hurl/src/runner/error.rs @@ -32,6 +32,16 @@ pub struct Error { pub assert: bool, } +impl Error { + pub fn new(source_info: SourceInfo, inner: RunnerError, assert: bool) -> Error { + Error { + source_info, + inner, + assert, + } + } +} + #[derive(Clone, Debug, PartialEq, Eq)] pub enum RunnerError { AssertBodyValueError { diff --git a/packages/hurl/src/runner/expr.rs b/packages/hurl/src/runner/expr.rs index 22d893cf53e..9243fb64a9e 100644 --- a/packages/hurl/src/runner/expr.rs +++ b/packages/hurl/src/runner/expr.rs @@ -28,12 +28,9 @@ pub fn eval_expr(expr: &Expr, variables: &HashMap) -> Result Ok(Some(Value::Number(Number::Integer(values.len() as i64)))), Value::Bytes(values) => Ok(Some(Value::Number(Number::Integer(values.len() as i64)))), Value::Nodeset(size) => Ok(Some(Value::Number(Number::Integer(*size as i64)))), - 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/days_after_now.rs b/packages/hurl/src/runner/filter/days_after_now.rs index a2087798060..db410f87d3c 100644 --- a/packages/hurl/src/runner/filter/days_after_now.rs +++ b/packages/hurl/src/runner/filter/days_after_now.rs @@ -32,11 +32,10 @@ pub fn eval_days_after_now( let diff = value.signed_duration_since(Utc::now()); Ok(Some(Value::Number(Number::Integer(diff.num_days())))) } - 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/days_before_now.rs b/packages/hurl/src/runner/filter/days_before_now.rs index 1eee6b45257..7a2bf47dad1 100644 --- a/packages/hurl/src/runner/filter/days_before_now.rs +++ b/packages/hurl/src/runner/filter/days_before_now.rs @@ -32,11 +32,10 @@ pub fn eval_days_before_now( let diff = Utc::now().signed_duration_since(*value); Ok(Some(Value::Number(Number::Integer(diff.num_days())))) } - 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/decode.rs b/packages/hurl/src/runner/filter/decode.rs index 9e536877c36..a2107f85521 100644 --- a/packages/hurl/src/runner/filter/decode.rs +++ b/packages/hurl/src/runner/filter/decode.rs @@ -35,26 +35,23 @@ pub fn eval_decode( match value { Value::Bytes(value) => { match encoding::label::encoding_from_whatwg_label(encoding_value.as_str()) { - None => Err(Error { - source_info, - inner: RunnerError::FilterInvalidEncoding(encoding_value), - assert, - }), + None => { + let inner = RunnerError::FilterInvalidEncoding(encoding_value); + Err(Error::new(source_info, inner, assert)) + } Some(enc) => match enc.decode(value, DecoderTrap::Strict) { Ok(decoded) => Ok(Some(Value::String(decoded))), - Err(_) => Err(Error { - source_info, - inner: RunnerError::FilterDecode(encoding_value), - assert, - }), + Err(_) => { + let inner = RunnerError::FilterDecode(encoding_value); + 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/eval.rs b/packages/hurl/src/runner/filter/eval.rs index 5ba7b7c88f2..5232a5856ed 100644 --- a/packages/hurl/src/runner/filter/eval.rs +++ b/packages/hurl/src/runner/filter/eval.rs @@ -54,11 +54,11 @@ pub fn eval_filters( value = if let Some(value) = value { eval_filter(filter, &value, variables, in_assert)? } else { - return Err(Error { - source_info: filter.source_info, - inner: RunnerError::FilterMissingInput, - assert: in_assert, - }); + return Err(Error::new( + filter.source_info, + RunnerError::FilterMissingInput, + in_assert, + )); } } Ok(value) diff --git a/packages/hurl/src/runner/filter/format.rs b/packages/hurl/src/runner/filter/format.rs index aa59fe150df..20601bcdf36 100644 --- a/packages/hurl/src/runner/filter/format.rs +++ b/packages/hurl/src/runner/filter/format.rs @@ -36,11 +36,10 @@ pub fn eval_format( let formatted = format!("{}", value.format(fmt.as_str())); Ok(Some(Value::String(formatted))) } - 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)) + } } }