Skip to content

Commit

Permalink
Use Error::new when possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcamiel committed Nov 27, 2023
1 parent 5af040c commit cb40c7b
Show file tree
Hide file tree
Showing 24 changed files with 253 additions and 365 deletions.
9 changes: 3 additions & 6 deletions packages/hurl/src/output/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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));
}
Expand Down
22 changes: 10 additions & 12 deletions packages/hurl/src/report/junit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}],
Expand All @@ -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,
}],
Expand Down
22 changes: 10 additions & 12 deletions packages/hurl/src/report/junit/testcase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}],
Expand Down Expand Up @@ -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,
}],
Expand Down
5 changes: 2 additions & 3 deletions packages/hurl/src/runner/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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![],
Expand Down
9 changes: 4 additions & 5 deletions packages/hurl/src/runner/filter/html_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

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

Expand Down
24 changes: 10 additions & 14 deletions packages/hurl/src/runner/filter/jsonpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ pub fn eval_jsonpath(
) -> Result<Option<Value>, 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))
}
}
}

Expand All @@ -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,
};
Expand Down
30 changes: 14 additions & 16 deletions packages/hurl/src/runner/filter/nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ pub fn eval_nth(
) -> Result<Option<Value>, 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))
}
}
}

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

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

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

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

Expand Down
18 changes: 8 additions & 10 deletions packages/hurl/src/runner/filter/to_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i64>() {
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))
}
}
}

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

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

Expand Down
Loading

0 comments on commit cb40c7b

Please sign in to comment.