Debian Codegraph @

man/stacktrace.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/conditions.R
3 \name{stacktrace}
4 \alias{stacktrace}
5 \alias{captureStackTraces}
6 \alias{withLogErrors}
7 \alias{printError}
8 \alias{printStackTrace}
9 \alias{conditionStackTrace}
10 \alias{conditionStackTrace<-}
11 \alias{..stacktraceon..}
12 \alias{..stacktraceoff..}
13 \title{Stack trace manipulation functions}
14 \usage{
15 captureStackTraces(expr)
16
17 withLogErrors(
18 expr,
19 full = get_devmode_option("shiny.fullstacktrace", FALSE),
20 offset = getOption("shiny.stacktraceoffset", TRUE)
21 )
22
23 printError(
24 cond,
25 full = get_devmode_option("shiny.fullstacktrace", FALSE),
26 offset = getOption("shiny.stacktraceoffset", TRUE)
27 )
28
29 printStackTrace(
30 cond,
31 full = get_devmode_option("shiny.fullstacktrace", FALSE),
32 offset = getOption("shiny.stacktraceoffset", TRUE)
33 )
34
35 conditionStackTrace(cond)
36
37 conditionStackTrace(cond) <- value
38
39 ..stacktraceon..(expr)
40
41 ..stacktraceoff..(expr)
42 }
43 \arguments{
44 \item{expr}{The expression to wrap.}
45
46 \item{full}{If \code{TRUE}, then every element of \code{sys.calls()} will be
47 included in the stack trace. By default (\code{FALSE}), calls that Shiny
48 deems uninteresting will be hidden.}
49
50 \item{offset}{If \code{TRUE} (the default), srcrefs will be reassigned from
51 the calls they originated from, to the destinations of those calls. If
52 you're used to stack traces from other languages, this feels more
53 intuitive, as the definition of the function indicated in the call and the
54 location specified by the srcref match up. If \code{FALSE}, srcrefs will be
55 left alone (traditional R treatment where the srcref is of the callsite).}
56
57 \item{cond}{A condition that may have previously been annotated by
58 \code{captureStackTraces} (or \code{withLogErrors}).}
59
60 \item{value}{The stack trace value to assign to the condition.}
61 }
62 \value{
63 \code{printError} and \code{printStackTrace} return
64 \code{invisible()}. The other functions pass through the results of
65 \code{expr}.
66 }
67 \description{
68 Advanced (borderline internal) functions for capturing, printing, and
69 manipulating stack traces.
70 }
71 \details{
72 \code{captureStackTraces} runs the given \code{expr} and if any
73 \emph{uncaught} errors occur, annotates them with stack trace info for use
74 by \code{printError} and \code{printStackTrace}. It is not necessary to use
75 \code{captureStackTraces} around the same expression as
76 \code{withLogErrors}, as the latter includes a call to the former. Note
77 that if \code{expr} contains calls (either directly or indirectly) to
78 \code{try}, or \code{tryCatch} with an error handler, stack traces therein
79 cannot be captured unless another \code{captureStackTraces} call is
80 inserted in the interior of the \code{try} or \code{tryCatch}. This is
81 because these calls catch the error and prevent it from traveling up to the
82 condition handler installed by \code{captureStackTraces}.
83
84 \code{withLogErrors} captures stack traces and logs errors that
85 occur in \code{expr}, but does allow errors to propagate beyond this point
86 (i.e. it doesn't catch the error). The same caveats that apply to
87 \code{captureStackTraces} with regard to \code{try}/\code{tryCatch} apply
88 to \code{withLogErrors}.
89
90 \code{printError} prints the error and stack trace (if any) using
91 \code{warning(immediate.=TRUE)}. \code{printStackTrace} prints the stack
92 trace only.
93
94 \code{conditionStackTrace} and \verb{conditionStackTrace<-} are
95 accessor functions for getting/setting stack traces on conditions.
96
97 The two functions \code{..stacktraceon..} and
98 \code{..stacktraceoff..} have no runtime behavior during normal execution;
99 they exist only to create artifacts on the stack trace (sys.call()) that
100 instruct the stack trace pretty printer what parts of the stack trace are
101 interesting or not. The initial state is 1 and we walk from the outermost
102 call inwards. Each ..stacktraceoff.. decrements the state by one, and each
103 ..stacktraceon.. increments the state by one. Any stack trace frame whose
104 value is less than 1 is hidden, and finally, the ..stacktraceon.. and
105 ..stacktraceoff.. calls themselves are hidden too.
106 }
107 \examples{
108 # Keeps tryCatch and withVisible related calls off the
109 # pretty-printed stack trace
110
111 visibleFunction1 <- function() {
112 stop("Kaboom!")
113 }
114
115 visibleFunction2 <- function() {
116 visibleFunction1()
117 }
118
119 hiddenFunction <- function(expr) {
120 expr
121 }
122
123 # An example without ..stacktraceon/off.. manipulation.
124 # The outer "try" is just to prevent example() from stopping.
125 try({
126 # The withLogErrors call ensures that stack traces are captured
127 # and that errors that bubble up are logged using warning().
128 withLogErrors({
129 # tryCatch and withVisible are just here to add some noise to
130 # the stack trace.
131 tryCatch(
132 withVisible({
133 hiddenFunction(visibleFunction2())
134 })
135 )
136 })
137 })
138
139 # Now the same example, but with ..stacktraceon/off.. to hide some
140 # of the less-interesting bits (tryCatch and withVisible).
141 ..stacktraceoff..({
142 try({
143 withLogErrors({
144 tryCatch(
145 withVisible(
146 hiddenFunction(
147 ..stacktraceon..(visibleFunction2())
148 )
149 )
150 )
151 })
152 })
153 })
154
155
156 }
157 \keyword{internal}