Skip to content

Commit

Permalink
R: Warn if error is too long to print properly (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian authored May 3, 2024
1 parent a566ce5 commit 0bf8aae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions R/R/bridgestan.R
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ handle_error <- function(lib_name, err_msg, err_ptr, function_name) {
return(paste("Unknown error in", function_name))
} else {
.C("bs_free_error_msg_R", as.raw(err_ptr), PACKAGE = lib_name)
if (getOption("warning.length") < nchar(err_msg)) {
warning("BridgeStan error message too long to fully display. Consider increasing options(warning.length)")
}
return(err_msg)
}
}
Expand Down
11 changes: 8 additions & 3 deletions R/R/compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ get_bridgestan_path <- function() {
"environment variable, downloading version ", packageVersion("bridgestan"),
" to ", path))
get_bridgestan_src()
print("Done!")
})
num_files <- length(list.files(HOME_BRIDGESTAN))
if (num_files >= 5) {
warning(paste0("Found ", num_files, " different versions of BridgeStan in ",
HOME_BRIDGESTAN, ". Consider deleting old versions to save space."))
}
print("Done!")
}

return(path)
Expand Down Expand Up @@ -101,8 +101,13 @@ compile_model <- function(stan_file, stanc_args = NULL, make_args = NULL) {
})
res_attrs <- attributes(res)
if ("status" %in% names(res_attrs) && res_attrs$status != 0) {
stop(paste0("Compilation failed with error code ", res_attrs$status, "\noutput:\n",
paste(res, collapse = "\n")))
error_msg <- paste0("Compilation failed with error code ", res_attrs$status,
"\noutput:\n", paste(res, collapse = "\n"))

if (getOption("warning.length") < nchar(error_msg)) {
warning("BridgeStan error message too long to fully display. Consider increasing options(warning.length)")
}
stop(error_msg)
}

return(output)
Expand Down

0 comments on commit 0bf8aae

Please sign in to comment.