errors are a vector instead of a single string

master
Edward Shen 2019-05-03 01:05:02 -04:00
parent 17a0730275
commit f42c70b27e
Signed by: edward
GPG Key ID: F350507060ED6C90
3 changed files with 43 additions and 49 deletions

View File

@ -60,21 +60,23 @@
<p>{{ results.timestamp_str }}</p> <p>{{ results.timestamp_str }}</p>
</header> </header>
{% for group in results.groups -%} {% for group in results.groups -%}
<h2>{{ group.label }}</h2> <h2>{{ group.label }}</h2>
{% for status in group.endpoints -%} {% for status in group.endpoints -%}
<section> <section>
<aside class="indicator {% if status.status == 0 %}ok{% elif status.status == 1 %}warn{% else %}error{% endif %}"></aside> <aside class="indicator {% if status.status == 0 %}ok{% elif status.status == 1 %}warn{% else %}error{% endif %}"></aside>
<main> <main>
<div class="info"> <div class="info">
<h3>{{ status.endpoint }}</h3> <h3>{{ status.endpoint }}</h3>
<div class="spacer"></div> <div class="spacer"></div>
{% if status.status != 2 %}<p>{{ status.rtt }}</p>{% endif %} {% if status.status != 2 %}<p>{{ status.rtt }}</p>{% endif %}
</div> </div>
<a href="{{ status.location }}" target="_blank">{{ status.location }}</a> <a href="{{ status.location }}" target="_blank">{{ status.location }}</a>
{% if status.error %}<p class="error-msg">{{ status.error }}</p>{% endif %} {% for msg in status.errors -%}
</main> <p class="error-msg">{{ msg }}</p>
</section> {% endfor -%}
{% endfor -%} </main>
</section>
{% endfor -%}
{% endfor -%} {% endfor -%}
</body> </body>
</html> </html>

View File

@ -8,7 +8,7 @@ pub struct EndpointStatus {
pub location: String, pub location: String,
pub endpoint: String, pub endpoint: String,
pub rtt: Option<String>, pub rtt: Option<String>,
pub error: Option<String>, pub errors: Vec<String>,
} }
impl EndpointStatus { impl EndpointStatus {
@ -18,27 +18,27 @@ impl EndpointStatus {
location, location,
endpoint, endpoint,
rtt: Some(rtt), rtt: Some(rtt),
error: None, errors: vec![],
} }
} }
pub fn warn(location: String, endpoint: String, rtt: String, error: Option<String>) -> Self { pub fn warn(location: String, endpoint: String, rtt: String, errors: Vec<String>) -> Self {
EndpointStatus { EndpointStatus {
status: 1, status: 1,
location, location,
endpoint, endpoint,
rtt: Some(rtt), rtt: Some(rtt),
error, errors,
} }
} }
pub fn error(location: String, endpoint: String, error: Option<String>) -> Self { pub fn error(location: String, endpoint: String, errors: Vec<String>) -> Self {
EndpointStatus { EndpointStatus {
status: 2, status: 2,
location, location,
endpoint, endpoint,
rtt: None, rtt: None,
error, errors,
} }
} }
} }

View File

@ -62,43 +62,42 @@ fn get_result(
match ping_result { match ping_result {
Ok(mut res) => { Ok(mut res) => {
let res_body = res.text().expect("could not get body of request"); let res_body = res.text().expect("could not get body of request");
let mut error = None; let mut errors = vec![];
if res.status() != code { if res.status() != code {
error = append_err_msg( errors.push(format!(
error, "Status code mismatch: {} != {}",
format!( res.status().as_u16(),
"Status code mismatch: {} != {}", code
res.status().as_u16(), ));
code
),
);
} }
if let Some(expected_hash) = &endpoint.body_hash { if let Some(expected_hash) = &endpoint.body_hash {
let expected = from_hex(expected_hash).unwrap(); let expected = from_hex(expected_hash).unwrap();
let actual = digest(&SHA256, String::from(res_body).as_bytes()); let actual = digest(&SHA256, String::from(res_body).as_bytes());
if &expected != &actual.as_ref() { 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 { } else if !body.is_empty() && res_body != body {
error = append_err_msg( errors.push(format!(
error, "Body mismatch: {} != {}",
format!("Body mismatch: {} != {}", res_body.len(), body.len()), res_body.len(),
); body.len()
));
} }
if let Some(max_rtt) = endpoint.max_rtt { if let Some(max_rtt) = endpoint.max_rtt {
if rtt.num_milliseconds() > max_rtt { if rtt.num_milliseconds() > max_rtt {
error = append_err_msg( errors.push(format!(
error, "RTT too long: {} > {}s",
format!("RTT too long: {} > {}s", rtt_string, max_rtt as f64 / 1000.), rtt_string,
); max_rtt as f64 / 1000.
));
} }
} }
if error.is_some() { if !errors.is_empty() {
EndpointStatus::warn(url, label, rtt_string, error) EndpointStatus::warn(url, label, rtt_string, errors)
} else { } else {
EndpointStatus::ok(url, label, rtt_string) EndpointStatus::ok(url, label, rtt_string)
} }
@ -107,7 +106,7 @@ fn get_result(
if let Some(true) = endpoint.should_err { if let Some(true) = endpoint.should_err {
EndpointStatus::ok(url, label, rtt_string) EndpointStatus::ok(url, label, rtt_string)
} else { } else {
EndpointStatus::error(url, label, Some(format!("{}", e))) EndpointStatus::error(url, label, vec![format!("{}", e)])
} }
} }
} }
@ -124,10 +123,3 @@ fn get_url(base: &Option<String>, path: &String, port: Option<u16>) -> Result<St
} }
Ok(url.into_string()) Ok(url.into_string())
} }
fn append_err_msg(optional: Option<String>, to_append: String) -> Option<String> {
Some(match optional {
Some(e) => format!("{}\n{}", e, to_append),
None => to_append,
})
}