Skip to content

Commit a09c467

Browse files
committed
Refactor `parse_safely for a better stack trace
1 parent 445bf3e commit a09c467

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

R/parse.R

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@
1717
parse_safely <- function(text, ...) {
1818
tried_parsing <- rlang::try_fetch(
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

0 commit comments

Comments
 (0)