Skip to content

Commit 790d45b

Browse files
Merge pull request #1216 from olivroy/cli-links
Improve stack trace styler throws on parse error
2 parents 445bf3e + c6944ab commit 790d45b

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

R/io.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ transform_utf8_one <- function(path, fun, dry) {
5454
if (inherits(e, "dryError")) {
5555
rlang::abort(conditionMessage(e))
5656
}
57-
warn(paste0("When processing ", path, ": ", conditionMessage(e)))
57+
show_path <- cli::style_hyperlink(
58+
cli::col_blue(basename(path)),
59+
paste0("file://", path)
60+
)
61+
cli::cli_warn("When processing {show_path}:", parent = e)
5862
NA
5963
}
6064
)

R/parse.R

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,26 @@
1515
#'
1616
#' styler:::parse_safely("a + 3 -4 -> \n glück + 1")
1717
parse_safely <- function(text, ...) {
18-
tried_parsing <- rlang::try_fetch(
18+
tried_parsing <- withCallingHandlers(
1919
parse(text = text, ...),
20-
error = function(e) e,
21-
warning = function(w) w
22-
)
23-
if (inherits(tried_parsing, "error")) {
24-
if (has_crlf_as_first_line_sep(tried_parsing$message, text)) {
25-
abort(paste0(
26-
"The code to style seems to use Windows style line endings (CRLF). ",
27-
"styler currently only supports Unix style line endings (LF). ",
28-
"Please change the EOL character in your editor to Unix style and try ",
29-
"again.\nThe parsing error was:\n", tried_parsing$message
30-
))
31-
} else {
32-
abort(tried_parsing$message)
20+
error = function(e) {
21+
if (has_crlf_as_first_line_sep(e$message, text)) {
22+
msg <- c(
23+
x = "The code to style seems to use Windows style line endings (CRLF).",
24+
`!` = "styler currently only supports Unix style line endings (LF). ",
25+
i = "Please change the EOL character in your editor to Unix style
26+
and try again."
27+
)
28+
} else {
29+
msg <- c(x = "Styling failed")
30+
}
31+
cli::cli_abort(msg, parent = e, call = NULL)
32+
},
33+
warning = function(w) {
34+
cli::cli_warn(w$message)
35+
w
3336
}
34-
} else if (inherits(tried_parsing, "warning")) {
35-
warn(tried_parsing$message)
36-
}
37+
)
3738
tried_parsing
3839
}
3940

R/testing.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ transform_and_check <- function(in_item, out_item,
117117
unclass()
118118
if (!file.exists(out_item)) {
119119
warn(paste(
120-
"File", out_item, "does not exist. Creating it from transormation."
120+
"File", out_item, "does not exist. Creating it from transformation."
121121
))
122122
file.create(out_item)
123123
}

0 commit comments

Comments
 (0)