Skip to content

Commit

Permalink
Add new constructor method on runner error.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcamiel committed Nov 27, 2023
1 parent 3752fe4 commit 5af040c
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 107 deletions.
37 changes: 12 additions & 25 deletions packages/hurl/src/runner/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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))
}
}
},
Expand All @@ -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)),
Expand Down
18 changes: 6 additions & 12 deletions packages/hurl/src/runner/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,16 @@ pub fn eval_file(filename: &Filename, context_dir: &ContextDir) -> Result<Vec<u8
// is a child of the context directory.
let file = filename.value.clone();
if !context_dir.is_access_allowed(&file) {
return Err(Error {
source_info: filename.source_info,
inner: RunnerError::UnauthorizedFileAccess {
path: PathBuf::from(file),
},
assert: false,
});
let inner = RunnerError::UnauthorizedFileAccess {
path: PathBuf::from(file),
};
return Err(Error::new(filename.source_info, inner, false));
}
let resolved_file = context_dir.get_path(&file);
let inner = RunnerError::FileReadAccess { value: file };
match std::fs::read(resolved_file) {
Ok(value) => 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)),
}
}

Expand Down
20 changes: 10 additions & 10 deletions packages/hurl/src/runner/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
19 changes: 6 additions & 13 deletions packages/hurl/src/runner/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
Expand Down Expand Up @@ -208,11 +205,7 @@ fn asserts_to_errors(asserts: &[AssertResult]) -> Vec<Error> {
.map(
|Error {
source_info, inner, ..
}| Error {
source_info,
inner,
assert: true,
},
}| Error::new(source_info, inner, true),
)
.collect()
}
Expand Down
10 changes: 10 additions & 0 deletions packages/hurl/src/runner/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 4 additions & 7 deletions packages/hurl/src/runner/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ pub fn eval_expr(expr: &Expr, variables: &HashMap<String, Value>) -> Result<Valu
if let Some(value) = variables.get(expr.variable.name.as_str()) {
Ok(value.clone())
} else {
Err(Error {
source_info: expr.variable.source_info,
inner: RunnerError::TemplateVariableNotDefined {
name: expr.variable.name.clone(),
},
assert: false,
})
let inner = RunnerError::TemplateVariableNotDefined {
name: expr.variable.name.clone(),
};
Err(Error::new(expr.variable.source_info, inner, false))
}
}
9 changes: 4 additions & 5 deletions packages/hurl/src/runner/filter/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ pub fn eval_count(
Value::List(values) => 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))
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions packages/hurl/src/runner/filter/days_after_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions packages/hurl/src/runner/filter/days_before_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

Expand Down
27 changes: 12 additions & 15 deletions packages/hurl/src/runner/filter/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions packages/hurl/src/runner/filter/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions packages/hurl/src/runner/filter/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

Expand Down

0 comments on commit 5af040c

Please sign in to comment.