Skip to content

Commit

Permalink
Use cli errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Oct 31, 2023
1 parent b617333 commit 7034774
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion R/debug.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ debug_levels <- c(
get_debug_level <- function() {
lvl <- tolower(Sys.getenv("ASCIICAST_DEBUG_LEVEL", "fatal"))
if (!lvl %in% names(debug_levels)) {
stop(cli::format_error(c(
throw(cli::format_error(c(
"Invalid asciicast debug level in {.env ASCIICAST_DEBUG_LEVEL env
var: {.code lvl}.",
i = "It must be one of {.code {names(debug_levels)}}."
Expand Down
4 changes: 3 additions & 1 deletion R/frames.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ load_frames <- function(cast, height = NA, width = NA) {

ct <- load_svg_term()
ldjs <- system.file("load-cast.js", package = "asciicast")
if (ldjs == "") stop("Internal error, cannot find 'load-cast.js'")
if (ldjs == "") {
throw(cli::format_error("Internal error, cannot find {.file load-cast.js}."))
}
ct$source(ldjs)

ct$assign("json", json)
Expand Down
2 changes: 1 addition & 1 deletion R/gif.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ write_gif <- function(cast, path, show = NULL, cols = NULL,
optimize = TRUE) {
with_cli_process("Finding phantom.js", {
phexe <- find_phantom()
if (is.null(phexe)) stop("No phantom.js, exiting.")
if (is.null(phexe)) throw(cli::format_error("No phantom.js, exiting."))
})

frames <- load_frames(cast)
Expand Down
7 changes: 3 additions & 4 deletions R/install-phantomjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ install_phantomjs <- function(version = "2.1.1",
}
unlink(c(zipdir, zipfile), recursive = TRUE)
if (!success) {
stop(
"Unable to install PhantomJS to any of these dirs: ",
paste(dirs, collapse = ", ")
)
throw(cli::format_error(c(
"Unable to install PhantomJS to {?/any of} {?this/these} dir{?s}: {.or {.path {dirs}}}."
)))
}
if (!quiet) {
message("phantomjs has been installed to ", normalizePath(destdir))
Expand Down
4 changes: 2 additions & 2 deletions R/knitr.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ eng_asciicast <- function(options) {

proc <- .GlobalEnv$.knitr_asciicast_process
if (!is.null(proc) && !proc$is_alive()) {
stop("asciicast subprocess crashed")
throw(cli::format_error("asciicast subprocess crashed"))
}
cast <- record(
cast_file,
Expand Down Expand Up @@ -150,7 +150,7 @@ eng_asciicastcpp11 <- function(options) {

proc <- .GlobalEnv$.knitr_asciicast_process
if (!is.null(proc) && !proc$is_alive()) {
stop("asciicast subprocess crashed") # nocov
throw(cli::format_error("asciicast subprocess crashed")) # nocov
}

cast <- record(cast_file, process = proc)
Expand Down
2 changes: 1 addition & 1 deletion R/merge.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ merge_casts <- function(...) {
casts <- lapply(list(...), handle_merge_cast)
types <- map_chr(casts, "[[", "type")
if (!"cast" %in% types) {
stop("You need to include at least one cast in `merge_cast()`.")
throw(cli::format_error("You need to include at least one cast in {.fn merge_casts}."))
}
wconf <- which(types == "cast")[[1]]
new_cast <- structure(
Expand Down
11 changes: 5 additions & 6 deletions R/svg.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ is_svg_supported <- function() {

check_svg_support <- function() {
if (!is_svg_supported()) {
stop(
"Writing SVG files needs a more recent Node library, see the ",
"documentation of the V8 package, e.g. ",
"https://github.com/jeroen/v8#readme"
)
throw(cli::format_error(c(
"Writing SVG files needs a more recent Node library.",
i = "See the documentation of the V8 package: {.url https://github.com/jeroen/v8#readme}."
)))
}
}

Expand Down Expand Up @@ -438,7 +437,7 @@ play_svg <- function(path) {
} else {
tmpsvg <- tempfile("asciicast", fileext = ".svg")
if (!file.copy(path, tmpsvg)) {
stop(cli::format_error(c(
throw(cli::format_error(c(
"Cannot copy {.path {path}} to {.path {tmpsvg}}."
)))
}
Expand Down
7 changes: 6 additions & 1 deletion R/write-json.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
as_json <- function(cast) {
stopifnot(inherits(cast, "asciicast"))
if (!inherits(cast, "asciicast")) {
throw(cli::format_error(c(
"{.var cast} must be an {.cls asciicast}, object.",
i = "It is a {.type {cast}}."
)))
}
con <- textConnection(NULL, "w", local = TRUE)
on.exit(close(con), add = TRUE)
write_json_con(cast, con)
Expand Down

0 comments on commit 7034774

Please sign in to comment.