Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling and logging of unexpected errors #44

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion portage/L0.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ overwrites <- 0
errors <- 0

f <- function(fn, new_dir) {

basefn <- basename(fn)
message(Sys.time(), " Processing ", basefn)
# Try to read in the data; if it doesn't work, we won't get a data frame back
Expand Down Expand Up @@ -119,7 +120,15 @@ f <- function(fn, new_dir) {
Note = note)
}

out <- lapply(files_to_process, f, new_dir = L0)
log_info("About to L0", logfile = params$logfile)
tryCatch({
out <- lapply(files_to_process, f, new_dir = L0)
},
error = function(e) {
log_warning("L0: an error occurred!", logfile = params$logfile)
log_info(as.character(e), logfile = params$logfile)
stop(e)
})
```

## Summary
Expand Down
8 changes: 8 additions & 0 deletions portage/L1_normalize.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,17 @@ f <- function(fn, out_dir, design_table) {
return(smry)
}

log_info("About to L1_normalize", logfile = params$logfile)
tryCatch({
out <- lapply(files_to_process, f,
out_dir = L1_NORMALIZE,
design_table = dt_ex)
},
error = function(e) {
log_warning("L1_normalize: an error occurred!", logfile = params$logfile)
log_info(as.character(e), logfile = params$logfile)
stop(e)
})
```

## Summary
Expand Down
8 changes: 8 additions & 0 deletions portage/L1a.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,16 @@ f <- function(dir_name, dirs_to_process, out_dir) {
return(smry)
}

log_info("About to L1a", logfile = params$logfile)
tryCatch({
out <- lapply(names(dirs_to_process), f,
dirs_to_process = dirs_to_process, out_dir = L1A)
},
error = function(e) {
log_warning("L1a: an error occurred!", logfile = params$logfile)
log_info(as.character(e), logfile = params$logfile)
stop(e)
})
```

## File summary
Expand Down
8 changes: 8 additions & 0 deletions portage/L1b.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,16 @@ f <- function(dir_name, dirs_to_process, out_dir) {
return(smry)
}

log_info("About to L1b", logfile = params$logfile)
tryCatch({
out <- lapply(names(dirs_to_process), f,
dirs_to_process = dirs_to_process, out_dir = L1B)
},
error = function(e) {
log_warning("L1b: an error occurred!", logfile = params$logfile)
log_info(as.character(e), logfile = params$logfile)
stop(e)
})
```

## Summary
Expand Down
43 changes: 36 additions & 7 deletions portage/driver.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,31 @@ now_string <- function() format(Sys.time(), "%Y%m%d.%H%M")

ROOT <- "./data_TEST"

# Log file ----------------------------------------------------

LOGS <- file.path(ROOT, "Logs/")

# Main logfile
LOGFILE <- file.path(LOGS, paste0("driver_log_", now_string(), ".txt"))
if(file.exists(LOGFILE)) file.remove(LOGFILE)

# Error handling ----------------------------------------------

STOP_ON_ERROR <- TRUE
ERROR_OCCURRED <- FALSE

# driver_try: ensure that if an *unexpected* error occurs,
# it's captured in the driver log file, and a flag is set
driver_try <- function(...) {
tryCatch(eval(...),
error = function(e) {
ERROR_OCCURRED <<- TRUE
log_warning("Driver: an error occurred!")
log_info(as.character(e))
if(STOP_ON_ERROR) stop(e)
}
)
}

# Construct L0 data ---------------------------------------------
# L0 data are raw but in CSV form, and with "Logger" and "Table" columns added
Expand All @@ -37,10 +56,12 @@ new_section("Starting L0")
outfile <- paste0("L0_", now_string(), ".html")
outfile <- file.path(LOGS, outfile)

quarto_render("L0.qmd",
execute_params = list(DATA_ROOT = ROOT,
html_outfile = outfile,
logfile = LOGFILE))
driver_try(
quarto_render("L0.qmd",
execute_params = list(DATA_ROOT = ROOT,
html_outfile = outfile,
logfile = LOGFILE))
)
copy_output("L0.html", outfile)


Expand All @@ -55,10 +76,12 @@ dt <- file.path(ROOT, "design_table.csv")
outfile <- paste0("L1_normalize_", now_string(), ".html")
outfile <- file.path(LOGS, outfile)

quarto_render("L1_normalize.qmd",
driver_try(
quarto_render("L1_normalize.qmd",
execute_params = list(DATA_ROOT = ROOT,
html_outfile = outfile,
logfile = LOGFILE))
)
copy_output("L1_normalize.html", outfile)


Expand All @@ -76,10 +99,12 @@ new_section("Starting L1a")
outfile <- paste0("L1a_", now_string(), ".html")
outfile <- file.path(LOGS, outfile)

quarto_render("L1a.qmd",
driver_try(
quarto_render("L1a.qmd",
execute_params = list(DATA_ROOT = ROOT,
html_outfile = outfile,
logfile = LOGFILE))
)
copy_output("L1a.html", outfile)


Expand All @@ -106,11 +131,15 @@ pt <- file.path(ROOT, "plot_table.csv")
outfile <- paste0("L1b_", now_string(), ".html")
outfile <- file.path(LOGS, outfile)

# quarto_render("L1b.qmd",
# driver_try(
# quarto_render("L1b.qmd",
# execute_params = list(DATA_ROOT = ROOT,
# html_outfile = outfile,
# logfile = LOGFILE))
# )
# copy_output("L1b.html", outfile)


if(ERROR_OCCURRED) warning ("One or more errors occurred!")

message("All done.")