Sor RunnerError variant.

This commit is contained in:
jcamiel 2023-11-22 14:15:04 +01:00
parent d89b209ee7
commit 2c67bd2569
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC

View File

@ -22,6 +22,10 @@ use hurl_core::ast::SourceInfo;
use crate::http::{HttpError, RequestedHttpVersion}; use crate::http::{HttpError, RequestedHttpVersion};
use crate::runner::Value; use crate::runner::Value;
/// Represents a single instance of a runtime error, usually triggered by running a
/// [`hurl_core::ast::Entry`]. Running a Hurl content (see [`crate::runner::run`]) returns a list of
/// result for each entry. Each entry result can contain a list of [`Error`]. The runtime error variant
/// is defined in [`RunnerError`]
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error { pub struct Error {
pub source_info: SourceInfo, pub source_info: SourceInfo,
@ -31,44 +35,51 @@ pub struct Error {
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum RunnerError { pub enum RunnerError {
TemplateVariableNotDefined { AssertBodyValueError {
name: String, actual: String,
expected: String,
}, },
TemplateVariableInvalidType { AssertFailure {
name: String, actual: String,
value: String, expected: String,
expecting: String, type_mismatch: bool,
}, },
InvalidJson { AssertHeaderValueError {
value: String, actual: String,
}, },
InvalidUrl(String), AssertStatus {
InvalidUrlPrefix(String), actual: String,
},
HttpConnection(String), AssertVersion {
actual: String,
},
CouldNotParseResponse,
CouldNotResolveProxyName, CouldNotResolveProxyName,
CouldNotResolveHost(String), CouldNotResolveHost(String),
FailToConnect,
Timeout,
TooManyRedirect,
CouldNotParseResponse,
SslCertificate(String),
UnsupportedContentEncoding(String),
UnsupportedHttpVersion(RequestedHttpVersion),
CouldNotUncompressResponse(String), CouldNotUncompressResponse(String),
FailToConnect,
FileReadAccess { FileReadAccess {
value: String, value: String,
}, },
InvalidDecoding { FilterDecode(String),
charset: String, FilterInvalidEncoding(String),
FilterInvalidInput(String),
FilterMissingInput,
FilterRegexNoCapture,
HttpConnection(String),
InvalidJson {
value: String,
}, },
InvalidCharset { InvalidCharset {
charset: String, charset: String,
}, },
InvalidDecoding {
// Query charset: String,
},
InvalidRegex,
InvalidUrl(String),
InvalidUrlPrefix(String),
NoQueryResult,
QueryHeaderNotFound, QueryHeaderNotFound,
QueryCookieNotFound, QueryCookieNotFound,
QueryInvalidJsonpathExpression { QueryInvalidJsonpathExpression {
@ -77,47 +88,28 @@ pub enum RunnerError {
QueryInvalidXpathEval, QueryInvalidXpathEval,
QueryInvalidXml, QueryInvalidXml,
QueryInvalidJson, QueryInvalidJson,
NoQueryResult,
// Predicate
PredicateType, PredicateType,
PredicateValue(Value), PredicateValue(Value),
AssertFailure { SslCertificate(String),
actual: String, TemplateVariableNotDefined {
expected: String, name: String,
type_mismatch: bool,
}, },
InvalidRegex, TemplateVariableInvalidType {
name: String,
AssertHeaderValueError { value: String,
actual: String, expecting: String,
}, },
AssertBodyValueError { Timeout,
actual: String, TooManyRedirect,
expected: String, UnsupportedContentEncoding(String),
}, UnsupportedHttpVersion(RequestedHttpVersion),
AssertVersion {
actual: String,
},
AssertStatus {
actual: String,
},
UnrenderableVariable { UnrenderableVariable {
name: String, name: String,
value: String, value: String,
}, },
UnauthorizedFileAccess { UnauthorizedFileAccess {
path: PathBuf, path: PathBuf,
}, },
// Filter
FilterMissingInput,
FilterInvalidInput(String),
FilterRegexNoCapture,
FilterInvalidEncoding(String),
FilterDecode(String),
} }
/// Textual Output for runner errors /// Textual Output for runner errors
@ -128,106 +120,55 @@ impl hurl_core::error::Error for Error {
fn description(&self) -> String { fn description(&self) -> String {
match &self.inner { match &self.inner {
RunnerError::AssertBodyValueError { .. } => "Assert body value".to_string(),
RunnerError::AssertFailure { .. } => "Assert failure".to_string(),
RunnerError::AssertHeaderValueError { .. } => "Assert header value".to_string(),
RunnerError::AssertStatus { .. } => "Assert status code".to_string(),
RunnerError::AssertVersion { .. } => "Assert HTTP version".to_string(),
RunnerError::CouldNotParseResponse => "HTTP connection".to_string(),
RunnerError::CouldNotResolveHost(_) => "HTTP connection".to_string(),
RunnerError::CouldNotResolveProxyName => "HTTP connection".to_string(),
RunnerError::CouldNotUncompressResponse(..) => "Decompression error".to_string(),
RunnerError::FailToConnect => "HTTP connection".to_string(),
RunnerError::FileReadAccess { .. } => "File read access".to_string(),
RunnerError::FilterDecode { .. } => "Filter Error".to_string(),
RunnerError::FilterInvalidEncoding { .. } => "Filter Error".to_string(),
RunnerError::FilterInvalidInput { .. } => "Filter Error".to_string(),
RunnerError::FilterMissingInput => "Filter Error".to_string(),
RunnerError::FilterRegexNoCapture => "Filter Error".to_string(),
RunnerError::HttpConnection { .. } => "HTTP connection".to_string(),
RunnerError::InvalidCharset { .. } => "Invalid charset".to_string(),
RunnerError::InvalidDecoding { .. } => "Invalid decoding".to_string(),
RunnerError::InvalidJson { .. } => "Invalid JSON".to_string(),
RunnerError::InvalidRegex => "Invalid regex".to_string(),
RunnerError::InvalidUrl(..) => "Invalid URL".to_string(), RunnerError::InvalidUrl(..) => "Invalid URL".to_string(),
RunnerError::InvalidUrlPrefix(..) => "Invalid URL".to_string(), RunnerError::InvalidUrlPrefix(..) => "Invalid URL".to_string(),
RunnerError::TemplateVariableNotDefined { .. } => "Undefined variable".to_string(), RunnerError::NoQueryResult => "No query result".to_string(),
RunnerError::TemplateVariableInvalidType { .. } => "Invalid variable type".to_string(), RunnerError::PredicateType => "Assert - inconsistent predicate type".to_string(),
RunnerError::HttpConnection { .. } => "HTTP connection".to_string(),
RunnerError::CouldNotResolveProxyName => "HTTP connection".to_string(),
RunnerError::CouldNotResolveHost(_) => "HTTP connection".to_string(),
RunnerError::FailToConnect => "HTTP connection".to_string(),
RunnerError::Timeout => "HTTP connection".to_string(),
RunnerError::TooManyRedirect => "HTTP connection".to_string(),
RunnerError::CouldNotParseResponse => "HTTP connection".to_string(),
RunnerError::SslCertificate { .. } => "SSL certificate".to_string(),
RunnerError::PredicateValue { .. } => "Assert - predicate value failed".to_string(), RunnerError::PredicateValue { .. } => "Assert - predicate value failed".to_string(),
RunnerError::InvalidRegex => "Invalid regex".to_string(),
RunnerError::FileReadAccess { .. } => "File read access".to_string(),
RunnerError::QueryInvalidXml => "Invalid XML".to_string(),
RunnerError::QueryInvalidXpathEval => "Invalid XPath expression".to_string(),
RunnerError::QueryHeaderNotFound => "Header not found".to_string(),
RunnerError::QueryCookieNotFound => "Cookie not found".to_string(), RunnerError::QueryCookieNotFound => "Cookie not found".to_string(),
RunnerError::AssertHeaderValueError { .. } => "Assert header value".to_string(), RunnerError::QueryHeaderNotFound => "Header not found".to_string(),
RunnerError::AssertBodyValueError { .. } => "Assert body value".to_string(),
RunnerError::AssertVersion { .. } => "Assert HTTP version".to_string(),
RunnerError::AssertStatus { .. } => "Assert status code".to_string(),
RunnerError::QueryInvalidJson => "Invalid JSON".to_string(), RunnerError::QueryInvalidJson => "Invalid JSON".to_string(),
RunnerError::QueryInvalidJsonpathExpression { .. } => "Invalid JSONPath".to_string(), RunnerError::QueryInvalidJsonpathExpression { .. } => "Invalid JSONPath".to_string(),
RunnerError::PredicateType => "Assert - inconsistent predicate type".to_string(), RunnerError::QueryInvalidXml => "Invalid XML".to_string(),
RunnerError::InvalidDecoding { .. } => "Invalid decoding".to_string(), RunnerError::QueryInvalidXpathEval => "Invalid XPath expression".to_string(),
RunnerError::InvalidCharset { .. } => "Invalid charset".to_string(), RunnerError::SslCertificate { .. } => "SSL certificate".to_string(),
RunnerError::AssertFailure { .. } => "Assert failure".to_string(), RunnerError::TemplateVariableInvalidType { .. } => "Invalid variable type".to_string(),
RunnerError::TemplateVariableNotDefined { .. } => "Undefined variable".to_string(),
RunnerError::Timeout => "HTTP connection".to_string(),
RunnerError::TooManyRedirect => "HTTP connection".to_string(),
RunnerError::UnauthorizedFileAccess { .. } => "Unauthorized file access".to_string(),
RunnerError::UnrenderableVariable { .. } => "Unrenderable variable".to_string(), RunnerError::UnrenderableVariable { .. } => "Unrenderable variable".to_string(),
RunnerError::NoQueryResult => "No query result".to_string(),
RunnerError::UnsupportedContentEncoding(..) => "Decompression error".to_string(), RunnerError::UnsupportedContentEncoding(..) => "Decompression error".to_string(),
RunnerError::UnsupportedHttpVersion(..) => "Unsupported HTTP version".to_string(), RunnerError::UnsupportedHttpVersion(..) => "Unsupported HTTP version".to_string(),
RunnerError::CouldNotUncompressResponse(..) => "Decompression error".to_string(),
RunnerError::InvalidJson { .. } => "Invalid JSON".to_string(),
RunnerError::UnauthorizedFileAccess { .. } => "Unauthorized file access".to_string(),
RunnerError::FilterMissingInput => "Filter Error".to_string(),
RunnerError::FilterInvalidInput { .. } => "Filter Error".to_string(),
RunnerError::FilterRegexNoCapture => "Filter Error".to_string(),
RunnerError::FilterInvalidEncoding { .. } => "Filter Error".to_string(),
RunnerError::FilterDecode { .. } => "Filter Error".to_string(),
} }
} }
fn fixme(&self) -> String { fn fixme(&self) -> String {
match &self.inner { match &self.inner {
RunnerError::InvalidUrl(url) => format!("invalid URL <{url}>"),
RunnerError::InvalidUrlPrefix(url) => {
format!("URL <{url}> must start with http:// or https://")
}
RunnerError::TemplateVariableNotDefined { name } => {
format!("you must set the variable {name}")
}
RunnerError::TemplateVariableInvalidType {
value, expecting, ..
} => {
format!("expecting {expecting}, actual value is <{value}>")
}
RunnerError::HttpConnection(message) => message.to_string(),
RunnerError::CouldNotResolveProxyName => "could not resolve proxy name".to_string(),
RunnerError::CouldNotResolveHost(host) => format!("could not resolve host <{host}>"),
RunnerError::FailToConnect => "fail to connect".to_string(),
RunnerError::Timeout => "timeout has been reached".to_string(),
RunnerError::TooManyRedirect => "too many redirect".to_string(),
RunnerError::CouldNotParseResponse => "could not parse response".to_string(),
RunnerError::SslCertificate(description) => description.clone(),
RunnerError::AssertVersion { actual, .. } => format!("actual value is <{actual}>"),
RunnerError::AssertStatus { actual, .. } => format!("actual value is <{actual}>"),
RunnerError::PredicateValue(value) => {
format!("actual value is <{value}>")
}
RunnerError::InvalidRegex => "regex expression is not valid".to_string(),
RunnerError::FileReadAccess { value } => format!("file {value} can not be read"),
RunnerError::QueryInvalidXml => "the HTTP response is not a valid XML".to_string(),
RunnerError::QueryHeaderNotFound => {
"this header has not been found in the response".to_string()
}
RunnerError::QueryCookieNotFound => {
"this cookie has not been found in the response".to_string()
}
RunnerError::QueryInvalidXpathEval => "the XPath expression is not valid".to_string(),
RunnerError::AssertHeaderValueError { actual } => {
format!("actual value is <{actual}>")
}
RunnerError::AssertBodyValueError { actual, .. } => { RunnerError::AssertBodyValueError { actual, .. } => {
format!("actual value is <{actual}>") format!("actual value is <{actual}>")
} }
RunnerError::QueryInvalidJson => "the HTTP response is not a valid JSON".to_string(),
RunnerError::QueryInvalidJsonpathExpression { value } => {
format!("the JSONPath expression '{value}' is not valid")
}
RunnerError::PredicateType => {
"predicate type inconsistent with value return by query".to_string()
}
RunnerError::InvalidDecoding { charset } => {
format!("the body can not be decoded with charset '{charset}'")
}
RunnerError::InvalidCharset { charset } => {
format!("the charset '{charset}' is not valid")
}
RunnerError::AssertFailure { RunnerError::AssertFailure {
actual, actual,
expected, expected,
@ -241,39 +182,89 @@ impl hurl_core::error::Error for Error {
}; };
format!("actual: {actual}\nexpected: {expected}{additional}") format!("actual: {actual}\nexpected: {expected}{additional}")
} }
RunnerError::UnrenderableVariable { name, value } => { RunnerError::AssertHeaderValueError { actual } => {
format!("variable <{name}> with value {value} can not be rendered") format!("actual value is <{actual}>")
} }
RunnerError::NoQueryResult => "The query didn't return any result".to_string(), RunnerError::AssertStatus { actual, .. } => format!("actual value is <{actual}>"),
RunnerError::UnsupportedContentEncoding(algorithm) => { RunnerError::AssertVersion { actual, .. } => format!("actual value is <{actual}>"),
format!("compression {algorithm} is not supported") RunnerError::CouldNotParseResponse => "could not parse response".to_string(),
} RunnerError::CouldNotResolveHost(host) => format!("could not resolve host <{host}>"),
RunnerError::UnsupportedHttpVersion(version) => { RunnerError::CouldNotResolveProxyName => "could not resolve proxy name".to_string(),
format!("{version} is not supported, check --version")
}
RunnerError::CouldNotUncompressResponse(algorithm) => { RunnerError::CouldNotUncompressResponse(algorithm) => {
format!("could not uncompress response with {algorithm}") format!("could not uncompress response with {algorithm}")
} }
RunnerError::FailToConnect => "fail to connect".to_string(),
RunnerError::FileReadAccess { value } => format!("file {value} can not be read"),
RunnerError::FilterDecode(encoding) => {
format!("value can not be decoded with <{encoding}> encoding")
}
RunnerError::FilterInvalidEncoding(encoding) => {
format!("<{encoding}> encoding is not supported")
}
RunnerError::FilterInvalidInput(message) => {
format!("invalid filter input: {message}")
}
RunnerError::FilterMissingInput => "missing value to apply filter".to_string(),
RunnerError::FilterRegexNoCapture => "capture not found".to_string(),
RunnerError::HttpConnection(message) => message.to_string(),
RunnerError::InvalidCharset { charset } => {
format!("the charset '{charset}' is not valid")
}
RunnerError::InvalidDecoding { charset } => {
format!("the body can not be decoded with charset '{charset}'")
}
RunnerError::InvalidJson { value } => { RunnerError::InvalidJson { value } => {
format!("actual value is <{value}>") format!("actual value is <{value}>")
} }
RunnerError::InvalidRegex => "regex expression is not valid".to_string(),
RunnerError::InvalidUrl(url) => format!("invalid URL <{url}>"),
RunnerError::InvalidUrlPrefix(url) => {
format!("URL <{url}> must start with http:// or https://")
}
RunnerError::NoQueryResult => "The query didn't return any result".to_string(),
RunnerError::PredicateType => {
"predicate type inconsistent with value return by query".to_string()
}
RunnerError::PredicateValue(value) => {
format!("actual value is <{value}>")
}
RunnerError::QueryCookieNotFound => {
"this cookie has not been found in the response".to_string()
}
RunnerError::QueryHeaderNotFound => {
"this header has not been found in the response".to_string()
}
RunnerError::QueryInvalidJson => "the HTTP response is not a valid JSON".to_string(),
RunnerError::QueryInvalidJsonpathExpression { value } => {
format!("the JSONPath expression '{value}' is not valid")
}
RunnerError::QueryInvalidXml => "the HTTP response is not a valid XML".to_string(),
RunnerError::QueryInvalidXpathEval => "the XPath expression is not valid".to_string(),
RunnerError::SslCertificate(description) => description.clone(),
RunnerError::TemplateVariableInvalidType {
value, expecting, ..
} => {
format!("expecting {expecting}, actual value is <{value}>")
}
RunnerError::TemplateVariableNotDefined { name } => {
format!("you must set the variable {name}")
}
RunnerError::Timeout => "timeout has been reached".to_string(),
RunnerError::TooManyRedirect => "too many redirect".to_string(),
RunnerError::UnauthorizedFileAccess { path } => { RunnerError::UnauthorizedFileAccess { path } => {
format!( format!(
"unauthorized access to file {}, check --file-root option", "unauthorized access to file {}, check --file-root option",
path.to_str().unwrap() path.to_str().unwrap()
) )
} }
RunnerError::FilterMissingInput => "missing value to apply filter".to_string(), RunnerError::UnrenderableVariable { name, value } => {
RunnerError::FilterInvalidInput(message) => { format!("variable <{name}> with value {value} can not be rendered")
format!("invalid filter input: {message}")
} }
RunnerError::FilterRegexNoCapture => "capture not found".to_string(), RunnerError::UnsupportedContentEncoding(algorithm) => {
RunnerError::FilterInvalidEncoding(encoding) => { format!("compression {algorithm} is not supported")
format!("<{encoding}> encoding is not supported")
} }
RunnerError::FilterDecode(encoding) => { RunnerError::UnsupportedHttpVersion(version) => {
format!("value can not be decoded with <{encoding}> encoding") format!("{version} is not supported, check --version")
} }
} }
} }
@ -289,6 +280,8 @@ impl From<HttpError> for RunnerError {
} }
HttpError::InvalidCharset { charset } => RunnerError::InvalidCharset { charset }, HttpError::InvalidCharset { charset } => RunnerError::InvalidCharset { charset },
HttpError::InvalidDecoding { charset } => RunnerError::InvalidDecoding { charset }, HttpError::InvalidDecoding { charset } => RunnerError::InvalidDecoding { charset },
HttpError::InvalidUrl(url) => RunnerError::InvalidUrl(url),
HttpError::InvalidUrlPrefix(url) => RunnerError::InvalidUrlPrefix(url),
HttpError::Libcurl { code, description } => { HttpError::Libcurl { code, description } => {
RunnerError::HttpConnection(format!("({code}) {description}")) RunnerError::HttpConnection(format!("({code}) {description}"))
} }
@ -308,8 +301,6 @@ impl From<HttpError> for RunnerError {
HttpError::UnsupportedHttpVersion(version) => { HttpError::UnsupportedHttpVersion(version) => {
RunnerError::UnsupportedHttpVersion(version) RunnerError::UnsupportedHttpVersion(version)
} }
HttpError::InvalidUrl(url) => RunnerError::InvalidUrl(url),
HttpError::InvalidUrlPrefix(url) => RunnerError::InvalidUrlPrefix(url),
} }
} }
} }