-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy patherrors.R
60 lines (44 loc) · 1.46 KB
/
errors.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
renv_format_srcref <- function(srcref) {
srcfile <- attr(srcref, "srcfile")
if (inherits(srcfile, c("srcfilecopy", "srcfilealias"))) {
start <- srcref[7L]
end <- srcref[8L]
} else {
start <- srcref[1L]
end <- srcref[3L]
}
srclines <- getSrcLines(srcfile, start, end)
index <- regexpr("[^[:space:]]", srclines)
indent <- min(index)
code <- substring(srclines, indent)
n <- length(code)
postfix <- sprintf("at %s#%i", basename(srcfile$filename), srcref[1L])
code[n] <- paste(code[n], postfix)
code
}
renv_error_handler <- function(...) {
# first, format calls
calls <- head(sys.calls(), n = -1L)
formatted <- lapply(calls, function(call) {
srcref <- attr(call, "srcref", exact = TRUE)
if (!is.null(srcref)) {
formatted <- catch(renv_format_srcref(srcref))
if (!inherits(formatted, "error"))
return(formatted)
}
format(call)
})
# compute prefixes
numbers <- format(seq_along(formatted))
prefixes <- sprintf("%s: ", rev(numbers))
indent <- paste(rep.int(" ", min(nchar(prefixes))), collapse = "")
# attach prefixes + indent
annotated <- map_chr(seq_along(formatted), function(i) {
code <- formatted[[i]]
prefix <- c(prefixes[[i]], rep.int(indent, length(code) - 1L))
paste(prefix, code, sep = "", collapse = "\n")
})
header <- "Traceback (most recent calls first):"
contents <- paste(c(header, annotated), collapse = "\n")
writeLines(contents, con = stderr())
}