Debian Codegraph @

R/errors.R

raw
1
2 renv_error_format_srcref <- function(call, srcref) {
3
4 srcfile <- attr(srcref, "srcfile", exact = TRUE)
5
6 if (inherits(srcfile, c("srcfilecopy", "srcfilealias"))) {
7 start <- srcref[7L]
8 end <- srcref[8L]
9 } else {
10 start <- srcref[1L]
11 end <- srcref[3L]
12 }
13
14 srclines <- getSrcLines(srcfile, start, end)
15 index <- regexpr("[^[:space:]]", srclines)
16 indent <- min(index)
17 code <- substring(srclines, indent)
18
19 if (length(code) >= 8L) {
20 simplified <- renv_error_simplify(call)
21 if (!identical(simplified, call))
22 code <- format(simplified)
23 }
24
25 n <- length(code)
26 postfix <- sprintf("at %s#%i", basename(srcfile$filename), srcref[1L])
27 code[n] <- paste(code[n], postfix)
28
29 code
30
31 }
32
33 renv_error_simplify <- function(object) {
34
35 case(
36 is.function(object) ~ renv_error_simplify_function(object),
37 is.recursive(object) ~ renv_error_simplify_recursive(object),
38 TRUE ~ object
39 )
40
41 }
42
43 renv_error_simplify_function <- function(object) {
44 f <- function() {}
45 formals(f) <- formals(object)
46 body(f) <- quote({ ... })
47 f
48 }
49
50 renv_error_simplify_recursive <- function(object) {
51
52 longcall <- renv_call_matches(object, "{") && length(object) >= 8
53
54 if (longcall)
55 return(quote(...))
56
57 for (i in seq_along(object))
58 if (!is.null(object[[i]]))
59 object[[i]] <- renv_error_simplify(object[[i]])
60
61 object
62
63 }
64
65 renv_error_format <- function(calls, frames) {
66
67 # first, format calls
68 formatted <- lapply(calls, function(call) {
69
70 srcref <- attr(call, "srcref", exact = TRUE)
71 if (!is.null(srcref)) {
72 formatted <- catch(renv_error_format_srcref(call, srcref))
73 if (!inherits(formatted, "error"))
74 return(formatted)
75 }
76
77 if (is.function(call[[1]]))
78 return("<condition-handler>(...)")
79
80 format(renv_error_simplify(call))
81
82 })
83
84 # compute prefixes
85 numbers <- format(seq_along(formatted))
86 prefixes <- sprintf("%s: ", rev(numbers))
87
88 # generate indent
89 indent <- paste(rep.int(" ", min(nchar(prefixes))), collapse = "")
90
91 # attach prefixes + indent
92 annotated <- uapply(seq_along(formatted), function(i) {
93 code <- formatted[[i]]
94 prefix <- c(prefixes[[i]], rep.int(indent, length(code) - 1L))
95 paste(prefix, code, sep = "")
96 })
97
98 header <- "Traceback (most recent calls last):"
99 c(header, annotated)
100
101 }
102
103 renv_error_find <- function(calls, frames) {
104
105 for (i in rev(seq_along(frames))) {
106
107 fn <- sys.function(which = i)
108 if (!identical(fn, stop))
109 next
110
111 frame <- frames[[i]]
112 args <- frame[["args"]]
113 if (is.null(args) || empty(args))
114 next
115
116 first <- args[[1L]]
117 if (!inherits(first, "condition"))
118 next
119
120 return(first)
121
122 }
123
124 }
125
126 renv_error_handler <- function(...) {
127
128 calls <- head(sys.calls(), n = -1L)
129 frames <- head(sys.frames(), n = -1L)
130
131 error <- renv_error_find(calls, frames)
132 if (identical(error$traceback, FALSE))
133 return(character())
134
135 formatted <- renv_error_format(calls, frames)
136 caution(formatted)
137
138 formatted
139
140 }
141
142 the$traceback <- NULL
143
144 renv_error_capture <- function(e) {
145 calls <- head(sys.calls(), n = -2L)
146 frames <- head(sys.frames(), n = -2L)
147 traceback <- renv_error_format(calls, frames)
148 the$traceback <- traceback
149 }
150
151 renv_error_tag <- function(e) {
152 e$traceback <- the$traceback
153 e
154 }
155
156 renv_error_handler_call <- function() {
157 as.call(list(renv_error_handler))
158 }