Skip to content

Commit

Permalink
refine error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nanxstats committed Jan 6, 2022
1 parent 3370874 commit 648118d
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions R/collate.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ collate <- function(pkg = ".", ...) {
# where the return can be a list of file specs
lst_spec <- flatten_file_spec(lst_spec)
k <- length(lst_spec)
if (k == 0L) stop("Please provide at least one file specification")
if (k == 0L) stop("Must provide at least one file specification.", call. = FALSE)
lst_collection <- lapply(seq_len(k), function(i) fs_to_df(path, lst_spec[[i]]))
df <- do.call(rbind, lst_collection)
# remove duplicates
Expand Down Expand Up @@ -131,7 +131,7 @@ fs_to_df <- function(pkg_path, file_spec) {
#' @noRd
flatten_file_spec <- function(lst) {
if (!all(sapply(lapply(lst, class), `%in%`, c("file_spec", "list")))) {
stop("Input has objects that are not file specifications")
stop("All inputs must be file specification objects.", call. = FALSE)
}
do.call(c, lapply(lst, function(x) if (!is_file_spec(x)) flatten_file_spec(x) else list(x)))
}
Expand Down Expand Up @@ -195,7 +195,7 @@ create_fc_df <- function(nrow) {
#' @noRd
get_pkg_name <- function(path) {
path <- paste0(path, "/DESCRIPTION")
if (!file.exists(path)) stop("Package root does not have a DESCRIPTION file")
if (!file.exists(path)) stop("Can't find the `DESCRIPTION` file in package root.", call. = FALSE)
desc_file <- normalizePath(path, winslash = "/")
pkg_name <- unname(read.dcf(desc_file)[, "Package"])
pkg_name
Expand Down
8 changes: 4 additions & 4 deletions R/collection.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ merge.file_collection <- function(x, y, ...) {
lst_fc <- list(x, y, ...)

if (!all(sapply(lst_fc, is_file_collection))) {
stop("All inputs must be file collection objects")
stop("All inputs must be file collection objects.", call. = FALSE)
}
pkg_name <- unique(sapply(lst_fc, "[[", "pkg_name"))
if (length(pkg_name) != 1L) {
stop("Can only merge file collections for the same package")
stop("Can't merge file collections for different packages.", call. = FALSE)
}

rbind0 <- function(...) rbind(..., stringsAsFactors = FALSE)
Expand All @@ -146,7 +146,7 @@ merge.file_collection <- function(x, y, ...) {
df <- unique(df)
# ensure relative paths are not duplicated
if (anyDuplicated(df$"path_rel") != 0L) {
stop("Some files share single relative path but conflicting absolute paths")
stop("Some files share the same relative path but conflicting absolute paths.", call. = FALSE)
}

new_file_collection(pkg_name, df)
Expand Down Expand Up @@ -188,7 +188,7 @@ prune.file_collection <- function(x, path) {
if (anyNA(idx)) {
warning(
paste0(
"No matching files in file collection: ",
"Can't find files in file collection: ",
paste0(path[is.na(idx)], collapse = ", ")
)
)
Expand Down
4 changes: 2 additions & 2 deletions R/pack.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ pack <- function(..., output, quiet = FALSE) {
lst_fc <- list(...)
npkgs <- length(lst_fc)
pkg_names <- get_fc_pkg(lst_fc)
if (npkgs < 1L) stop("Please provide at least one file collection as input")
if (!all(sapply(lst_fc, is_file_collection))) stop("All inputs must be file collections")
if (npkgs < 1L) stop("Must provide at least one file collection as input.", call. = FALSE)
if (!all(sapply(lst_fc, is_file_collection))) stop("All inputs must be file collection objects.", call. = FALSE)

# determine output file name ----
output_default <- if (npkgs > 1L) "pkglite.txt" else tolower(paste0(pkg_names[1L], ".txt"))
Expand Down
2 changes: 1 addition & 1 deletion R/spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#' )
file_spec <- function(path, pattern = NULL, format = c("binary", "text"),
recursive = TRUE, ignore_case = TRUE, all_files = FALSE) {
if (missing(path)) stop("path cannot be empty")
if (missing(path)) stop("Must provide a non-empty `path`.", call. = FALSE)
format <- match.arg(format)

lst <- list(
Expand Down
4 changes: 2 additions & 2 deletions R/unpack.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
#' list.files()
unpack <- function(input, output = ".", install = FALSE, quiet = FALSE, ...) {
# handle input ----
if (missing(input)) stop("Please provide an input file")
if (missing(input)) stop("Must provide an input file.", call. = FALSE)

# read input ----
if (!quiet) cli_h1("Unpacking from pkglite file")
Expand Down Expand Up @@ -224,7 +224,7 @@ extract_value <- function(x) {
#'
#' @noRd
extract_value_multi <- function(x, start, end) {
if (length(start) != length(end)) stop("start and end must have equal length")
if (length(start) != length(end)) stop("`start` and `end` must have equal length.", call. = FALSE)
k <- length(start)
lapply(seq_len(k), function(i) substring(x[start[i]:end[i]], 3))
}
Expand Down
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#' pack(output = txt, quiet = TRUE) %>%
#' verify_ascii()
verify_ascii <- function(input, quiet = FALSE) {
if (missing(input)) stop("Please provide an input file")
if (missing(input)) stop("Must provide an input file.", call. = FALSE)
input <- file.path(input)
x <- suppressMessages(showNonASCIIfile(input))
has_only_ascii <- !as.logical(length(x))
Expand Down Expand Up @@ -91,7 +91,7 @@ verify_ascii <- function(input, quiet = FALSE) {
#' pack(output = txt, quiet = TRUE) %>%
#' remove_content(c("## New Features", "## Improvements"), quiet = TRUE)
remove_content <- function(input, x, quiet = FALSE) {
if (missing(input)) stop("Please provide an input file")
if (missing(input)) stop("Must provide an input file.", call. = FALSE)
y <- readLines(input)

idx_content <- which(substr(y, 1L, 2L) == " ")
Expand Down

0 comments on commit 648118d

Please sign in to comment.