Skip to content

Commit

Permalink
add error detail page
Browse files Browse the repository at this point in the history
  • Loading branch information
Adinata Wijaya committed Jul 28, 2023
1 parent 7a0c164 commit ac8ddb1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 43 deletions.
71 changes: 39 additions & 32 deletions src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,6 @@ pub fn write_external_detail(
actual: impl AsRef<Path>,
stdout: &str,
stderr: &str,
message: &str,
report_dir: impl AsRef<Path>,
) -> Result<Option<DetailPath>, Error> {
let detail_path = create_detail_folder(report_dir.as_ref())?;
Expand All @@ -484,7 +483,6 @@ pub fn write_external_detail(
ctx.insert("nominal", &nominal.as_ref().to_string_lossy());
ctx.insert("stdout", stdout);
ctx.insert("stderr", stderr);
ctx.insert("message", message);

let file = fat_io_wrap_std(&detail_file, &File::create)?;
debug!("detail html {:?} created", &detail_file);
Expand All @@ -497,7 +495,7 @@ pub fn write_external_detail(
fn create_error_detail(
nominal: impl AsRef<Path>,
actual: impl AsRef<Path>,
error: Box<dyn std::error::Error>,
errors: &[&String],
report_dir: impl AsRef<Path>,
) -> Result<DetailPath, Error> {
let sub_folder = create_detail_folder(report_dir.as_ref())?;
Expand All @@ -512,7 +510,7 @@ fn create_error_detail(
let mut ctx = Context::new();
ctx.insert("actual", &actual.as_ref().to_string_lossy());
ctx.insert("nominal", &nominal.as_ref().to_string_lossy());
ctx.insert("error", &error.to_string());
ctx.insert("errors", errors);

let file = fat_io_wrap_std(&detail_file, &File::create)?;

Expand All @@ -524,22 +522,14 @@ fn create_error_detail(
pub fn write_error_detail(
nominal: impl AsRef<Path>,
actual: impl AsRef<Path>,
error: Box<dyn std::error::Error>,
errors: &[&String],
report_dir: impl AsRef<Path>,
) -> RenderToHtmlDifference {
let mut result = RenderToHtmlDifference {
diff: Default::default(),
detail_path: None,
additional_columns: vec![],
};

if let Ok(sub_folder) = create_error_detail(nominal, actual, error, report_dir) {
result.detail_path = Some(sub_folder);
) -> Option<DetailPath> {
if let Ok(sub_folder) = create_error_detail(nominal, actual, errors, report_dir) {
Some(sub_folder)
} else {
error!("Could not create error detail");
None
}

result
}

pub(crate) fn create_reports(
Expand Down Expand Up @@ -593,15 +583,28 @@ pub(crate) fn create_html(
.diffs
.iter()
.map(|file| {
// let diffs: Vec<&DiffDetail> = file
// .detail
// .iter()
// .filter(|r| matches!(r, DiffDetail::Error(_)))
// .collect();

// TODO: Write diffs to report -> this is crate error?
let errors: Vec<&String> = file
.detail
.iter()
.filter_map(|r| match r {
DiffDetail::Error(s) => Some(s),
_ => None,
})
.collect();

if !errors.is_empty() {
return RenderToHtmlDifference {
diff: file.clone(),
detail_path: write_error_detail(
&file.nominal_file,
&file.actual_file,
&errors,
&sub_folder,
),
additional_columns: Vec::new(),
};
}

//TODO: use Unwrap_or_else display error in report + console
let detail_path = match &rule_difference.rule.file_type {
ComparisonMode::CSV(config) => {
let diffs: Vec<&DiffType> = file
Expand All @@ -620,7 +623,7 @@ pub(crate) fn create_html(
&config.delimiters,
&sub_folder,
)
.unwrap_or_default()
.unwrap_or_else(|e| log_detail_html_creation_error(&e))
}
ComparisonMode::PlainText(_) => {
let diffs: Vec<String> = file
Expand All @@ -646,7 +649,7 @@ pub(crate) fn create_html(
&diffs,
&sub_folder,
)
.unwrap_or_default()
.unwrap_or_else(|e| log_detail_html_creation_error(&e))
}
ComparisonMode::PDFText(_) => {
let diffs: Vec<(&usize, String)> =
Expand All @@ -670,7 +673,7 @@ pub(crate) fn create_html(
.collect();

write_pdf_detail(&file.nominal_file, &file.actual_file, &diffs, &sub_folder)
.unwrap_or_default()
.unwrap_or_else(|e| log_detail_html_creation_error(&e))
}
ComparisonMode::Image(_) => {
let diffs: Vec<(&f64, &String)> = file
Expand All @@ -690,7 +693,7 @@ pub(crate) fn create_html(
&diffs, //should actually only 1 image per file compare
&sub_folder,
)
.unwrap_or_default()
.unwrap_or_else(|e| log_detail_html_creation_error(&e))
}
ComparisonMode::External(_) => {
if let Some((stdout, stderr)) = file
Expand All @@ -707,10 +710,9 @@ pub(crate) fn create_html(
&file.actual_file,
stdout,
stderr,
"", //TODO: this is DiffDetail::Error
&sub_folder,
)
.unwrap_or_default()
.unwrap_or_else(|e| log_detail_html_creation_error(&e))
} else {
None
}
Expand All @@ -735,7 +737,7 @@ pub(crate) fn create_html(
&diffs,
&sub_folder,
)
.unwrap_or_default()
.unwrap_or_else(|e| log_detail_html_creation_error(&e))
}
};

Expand Down Expand Up @@ -871,6 +873,11 @@ pub(crate) fn get_relative_path(
PathBuf::from_iter(paths)
}

fn log_detail_html_creation_error(e: &Error) -> Option<DetailPath> {
error!("Could not create HTML-Detail: {}", e.to_string());
None
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
55 changes: 46 additions & 9 deletions src/report/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,26 @@ pub const ERROR_DETAIL_TEMPLATE: &str = r###"
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Error</title>
<style>
<title>Error(s)</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.12.1/datatables.min.css"/>
<style>
h3 {
background-color:black;
color:white;
padding:10px;
margin:10px 0;
cursor:pointer;
}
table {
table-layout: fixed;
}
table.dataTable#report tbody tr {
background-color: #fbcccc;
}
</style>
</head>
Expand All @@ -552,10 +569,34 @@ pub const ERROR_DETAIL_TEMPLATE: &str = r###"
</table>
</p>
<p style="color:red">
{{ error }}
</p>
<table id="report">
<thead>
<tr>
<th>Error</th>
</tr>
</thead>
<tbody>
{% for error in errors %}
<tr>
<td>{{ error }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.12.1/datatables.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
let table = new DataTable('#report', {
iDisplayLength: -1,
bPaginate: false,
bLengthChange: false,
bFilter: false,
bInfo: false
});
});
</script>
</body>
</html>
Expand Down Expand Up @@ -619,10 +660,6 @@ pub const PLAIN_EXTERNAL_DETAIL_TEMPLATE: &str = r###"
<h3>Compare Result of {{ actual }} and {{ nominal }}</h3>
<p class="has_error">
{{ message }}
</p>
<table id="compare">
<thead>
<tr>
Expand Down
4 changes: 2 additions & 2 deletions tests/integ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ fn simple_test_identity() {

#[test]
fn display_of_status_message_in_cm_tables() {
let report_dir = "/tmp/test_report/";
// tempfile::tempdir().expect("Could not generate temporary directory for report");
let report_dir =
tempfile::tempdir().expect("Could not generate temporary directory for report");

assert!(compare_folders(
"tests/integ/data/display_of_status_message_in_cm_tables/expected/",
Expand Down

0 comments on commit ac8ddb1

Please sign in to comment.