From f42c70b27ee31daf5088a76c3549e2c009339cd7 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Fri, 3 May 2019 01:05:02 -0400 Subject: [PATCH] errors are a vector instead of a single string --- config/templates/index.html | 32 +++++++++++++------------ src/results.rs | 12 +++++----- src/updater.rs | 48 ++++++++++++++++--------------------- 3 files changed, 43 insertions(+), 49 deletions(-) diff --git a/config/templates/index.html b/config/templates/index.html index ad91b58..b21c4e1 100644 --- a/config/templates/index.html +++ b/config/templates/index.html @@ -60,21 +60,23 @@

{{ results.timestamp_str }}

{% for group in results.groups -%} -

{{ group.label }}

- {% for status in group.endpoints -%} -
- -
-
-

{{ status.endpoint }}

-
- {% if status.status != 2 %}

{{ status.rtt }}

{% endif %} -
- {{ status.location }} - {% if status.error %}

{{ status.error }}

{% endif %} -
-
- {% endfor -%} +

{{ group.label }}

+ {% for status in group.endpoints -%} +
+ +
+
+

{{ status.endpoint }}

+
+ {% if status.status != 2 %}

{{ status.rtt }}

{% endif %} +
+ {{ status.location }} + {% for msg in status.errors -%} +

{{ msg }}

+ {% endfor -%} +
+
+ {% endfor -%} {% endfor -%} diff --git a/src/results.rs b/src/results.rs index 1d755db..7ca5581 100644 --- a/src/results.rs +++ b/src/results.rs @@ -8,7 +8,7 @@ pub struct EndpointStatus { pub location: String, pub endpoint: String, pub rtt: Option, - pub error: Option, + pub errors: Vec, } impl EndpointStatus { @@ -18,27 +18,27 @@ impl EndpointStatus { location, endpoint, rtt: Some(rtt), - error: None, + errors: vec![], } } - pub fn warn(location: String, endpoint: String, rtt: String, error: Option) -> Self { + pub fn warn(location: String, endpoint: String, rtt: String, errors: Vec) -> Self { EndpointStatus { status: 1, location, endpoint, rtt: Some(rtt), - error, + errors, } } - pub fn error(location: String, endpoint: String, error: Option) -> Self { + pub fn error(location: String, endpoint: String, errors: Vec) -> Self { EndpointStatus { status: 2, location, endpoint, rtt: None, - error, + errors, } } } diff --git a/src/updater.rs b/src/updater.rs index 52bf390..4afca52 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -62,43 +62,42 @@ fn get_result( match ping_result { Ok(mut res) => { let res_body = res.text().expect("could not get body of request"); - let mut error = None; + let mut errors = vec![]; if res.status() != code { - error = append_err_msg( - error, - format!( - "Status code mismatch: {} != {}", - res.status().as_u16(), - code - ), - ); + errors.push(format!( + "Status code mismatch: {} != {}", + res.status().as_u16(), + code + )); } if let Some(expected_hash) = &endpoint.body_hash { let expected = from_hex(expected_hash).unwrap(); let actual = digest(&SHA256, String::from(res_body).as_bytes()); if &expected != &actual.as_ref() { - error = append_err_msg(error, String::from("Body hash mismatch.")); + errors.push(String::from("Body hash mismatch.")); } } else if !body.is_empty() && res_body != body { - error = append_err_msg( - error, - format!("Body mismatch: {} != {}", res_body.len(), body.len()), - ); + errors.push(format!( + "Body mismatch: {} != {}", + res_body.len(), + body.len() + )); } if let Some(max_rtt) = endpoint.max_rtt { if rtt.num_milliseconds() > max_rtt { - error = append_err_msg( - error, - format!("RTT too long: {} > {}s", rtt_string, max_rtt as f64 / 1000.), - ); + errors.push(format!( + "RTT too long: {} > {}s", + rtt_string, + max_rtt as f64 / 1000. + )); } } - if error.is_some() { - EndpointStatus::warn(url, label, rtt_string, error) + if !errors.is_empty() { + EndpointStatus::warn(url, label, rtt_string, errors) } else { EndpointStatus::ok(url, label, rtt_string) } @@ -107,7 +106,7 @@ fn get_result( if let Some(true) = endpoint.should_err { EndpointStatus::ok(url, label, rtt_string) } else { - EndpointStatus::error(url, label, Some(format!("{}", e))) + EndpointStatus::error(url, label, vec![format!("{}", e)]) } } } @@ -124,10 +123,3 @@ fn get_url(base: &Option, path: &String, port: Option) -> Result, to_append: String) -> Option { - Some(match optional { - Some(e) => format!("{}\n{}", e, to_append), - None => to_append, - }) -}