metrumresearchgroup / bbr

R interface for model and project management
https://metrumresearchgroup.github.io/bbr/
Other
22 stars 2 forks source link

Better Error formatting #624

Open barrettk opened 7 months ago

barrettk commented 7 months ago

We have discussed the idea of moving to more informed and better formatted error messages using the cli package. At some point we should go through all the error messages and determine if any of them can be improved with either additional information, or through highlighting/formatting certain components. An example created during this PR can be seen below. We opted to revert these changes (context) in favor of not messing with the dependencies in that PR.

#' Confirm parent model path is valid, and error informatively if not
#'
#' @param .parent_mod Path to a parent model to inherit properties from.
#'
#' @keywords internal
validate_parent_mod <- function(.parent_mod){
  if(is.null(.parent_mod) || !fs::file_exists(.parent_mod)){
    mod_exists <- fs::file_exists(ctl_ext(.parent_mod)) ||
      fs::file_exists(mod_ext(.parent_mod))

    msg_info <- c("i" = "To inherit parameter estimates from a parent model, {.code .parent_mod}
        must be a file path to a previously executed model.")
    if(is.null(.parent_mod)){
      # If the model wasnt created via copy_model_from, or the `based_on` field
      # is otherwise empty
      msg_prefix <- glue::glue("{.code get_based_on(.mod)} did not return any parent models.
                 Please specify {.code .parent_mod} directly, or update the {.emph based_on}
                 attribute of `.mod`. See {.code ?add_based_on} for details.",
                 .open = "{{", .close = "}}")
      msg <- c("x" = msg_prefix)
    }else if(mod_exists && !fs::dir_exists(.parent_mod)){
      msg_prefix <- glue::glue("Parent model ({.code {{basename(ctl_ext(.parent_mod))}}}) exists,
                 but {.emph has not been executed.}", .open = "{{", .close = "}}")
      msg <- c("x" = msg_prefix, msg_info)
    }else{
      msg_prefix <- glue::glue("Parent model does not exist at: {.emph {{.parent_mod}}}", .open = "{{", .close = "}}")
      msg <- c("x" = msg_prefix, msg_info)
    }

    cli::cli_div(theme = list(span.emph = list(color = "red"), span.code = list(color = "blue")))
    cli::cli_abort(msg)
  }

  return(invisible(TRUE))
}