Skip to content

Commit

Permalink
Merge pull request #1216 from olivroy/cli-links
Browse files Browse the repository at this point in the history
Improve stack trace styler throws on parse error
  • Loading branch information
IndrajeetPatil authored Jun 15, 2024
2 parents 445bf3e + c6944ab commit 790d45b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
6 changes: 5 additions & 1 deletion R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ transform_utf8_one <- function(path, fun, dry) {
if (inherits(e, "dryError")) {
rlang::abort(conditionMessage(e))
}
warn(paste0("When processing ", path, ": ", conditionMessage(e)))
show_path <- cli::style_hyperlink(
cli::col_blue(basename(path)),
paste0("file://", path)
)
cli::cli_warn("When processing {show_path}:", parent = e)
NA
}
)
Expand Down
35 changes: 18 additions & 17 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,26 @@
#'
#' styler:::parse_safely("a + 3 -4 -> \n glück + 1")
parse_safely <- function(text, ...) {
tried_parsing <- rlang::try_fetch(
tried_parsing <- withCallingHandlers(
parse(text = text, ...),
error = function(e) e,
warning = function(w) w
)
if (inherits(tried_parsing, "error")) {
if (has_crlf_as_first_line_sep(tried_parsing$message, text)) {
abort(paste0(
"The code to style seems to use Windows style line endings (CRLF). ",
"styler currently only supports Unix style line endings (LF). ",
"Please change the EOL character in your editor to Unix style and try ",
"again.\nThe parsing error was:\n", tried_parsing$message
))
} else {
abort(tried_parsing$message)
error = function(e) {
if (has_crlf_as_first_line_sep(e$message, text)) {
msg <- c(
x = "The code to style seems to use Windows style line endings (CRLF).",
`!` = "styler currently only supports Unix style line endings (LF). ",
i = "Please change the EOL character in your editor to Unix style
and try again."
)
} else {
msg <- c(x = "Styling failed")
}
cli::cli_abort(msg, parent = e, call = NULL)
},
warning = function(w) {
cli::cli_warn(w$message)
w
}
} else if (inherits(tried_parsing, "warning")) {
warn(tried_parsing$message)
}
)
tried_parsing
}

Expand Down
2 changes: 1 addition & 1 deletion R/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ transform_and_check <- function(in_item, out_item,
unclass()
if (!file.exists(out_item)) {
warn(paste(
"File", out_item, "does not exist. Creating it from transormation."
"File", out_item, "does not exist. Creating it from transformation."
))
file.create(out_item)
}
Expand Down

0 comments on commit 790d45b

Please sign in to comment.