forked from tidyverse/style
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.Rmd
More file actions
237 lines (177 loc) · 7.18 KB
/
Copy patherrors.Rmd
File metadata and controls
237 lines (177 loc) · 7.18 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Error messages
```{r, include = FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
An error message should start with a general statement of the problem then give
a concise description of what went wrong. Consistent use of punctuation and
formatting makes errors easier to parse.
(This guide is currently almost entirely aspirational; most of the bad examples
come from existing tidyverse code.)
## Problem statement
Every error message should start with a general statement of the problem. It
should be concise, but informative. (This is hard!)
* If the cause of the problem is clear, use "must":
```{r}
dplyr::nth(1:10, "x")
#> Error: `n` must be a numeric vector, not a character vector.
dplyr::nth(1:10, 1:2)
#> Error: `n` must have length 1, not length 2.
```
Clear cut causes typically involve incorrect types or lengths.
* If you cannot state what was expected, use "can't":
```{r}
mtcars %>% pull(b)
#> Error: Can't find column `b` in `.data`.
as_vector(environment())
#> Error: Can't coerce `.x` to a vector.
purrr::modify_depth(list(list(x = 1)), 3, ~ . + 1)
#> Error: Can't find specified `.depth` in `.x`.
```
The problem statement should use sentence case and end with a full stop.
Use `stop(call. = FALSE)`, `rlang::abort()`, `Rf_errorcall(R_NilValue, ...)` to
avoid cluttering the error message with the name of the function that generated
it. That information is often not informative, and can easily be accessed via
`traceback()` or an IDE equivalent.
## Error location
Do your best to reveal the location, name, and/or content of the troublesome
component. The goal is to make it as easy as possible for the user to find and
fix the problem.
```{r}
# GOOD
map_int(1:5, ~ "x")
#> Error: Each result must be a single integer:
#> * Result 1 is a character vector.
# BAD
map_int(1:5, ~ "x")
#> Error: Each result must be a single integer
```
(It is often not easy to identify the exact problem; it may require passing
around extra arguments so that error messages generated at a lower-level can
know the original source. For frequently used functions, the effort is typically
worth it.)
If the source of the error is unclear, avoid pointing the user in the wrong
direction by giving an opinion about the source of the error:
```{r}
# GOOD
pull(mtcars, b)
#> Error: Can't find column `b` in `.data`.
tibble(x = 1:2, y = 1:3, z = 1)
#> Error: Columns must have consistent lengths:
#> * Column `x` has length 2
#> * Column `y` has length 3
# BAD: implies one argument at fault
pull(mtcars, b)
#> Error: Column `b` must exist in `.data`
pull(mtcars, b)
#> Error: `.data` must contain column `b`
tibble(x = 1:2, y = 1:3, z = 1)
#> Error: Column `x` must be length 1 or 3, not 2
```
If there are multiple issues, or an inconsistency revealed across several
arguments or items, prefer a bulleted list:
```{r}
# GOOD
purrr::reduce2(1:4, 1:2, `+`)
#> Error: `.x` and `.y` must have compatible lengths:
#> * `.x` has length 4
#> * `.y` has length 2
# BAD: harder to scan
purrr::reduce2(1:4, 1:2, `+`)
#> Error: `.x` and `.y` must have compatible lengths: `.x` has length 4 and
#> `.y` has length 2
```
If the list of issues might be long, make sure to truncate to only show the first few:
```{r}
# GOOD
#> Error: NAs found at 1,000,000 locations: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...
```
If you want to correctly pluralise the error message, consider using `ngettext()`. See the notes in `?ngettext()` for some challenges related to correct translation to other languages.
## Hints
If the source of the error is clear and common, you may want to provide a hint as
to how to fix it:
```{r}
dplyr::filter(iris, Species = "setosa")
#> Error: Filter specifications must be named.
#> Did you mean `Species == "setosa"`?
ggplot2::ggplot(ggplot2::aes())
#> Error: Can't plot data with class "uneval".
#> Did you accidentally provide the results of aes() to the `data` argument?
```
Hints should always end in a question mark.
Hints are particularly important if the source of the error is far away from the
root cause:
```{r}
# BAD
mean[[1]]
#> Error in mean[[1]] : object of type 'closure' is not subsettable
# BETTER
mean[[1]]
#> Error: Can't subset a function.
# BEST
mean[[1]]
#> Error: Can't subset a function.
#> Have you forgotten to define a variable named `mean`?
```
Good hints are difficult to write because, as above, you want to avoid steering
users in the wrong direction. Generally, I avoid writing a hint unless the
problem is common, and you can easily find a common pattern of incorrect usage
(e.g. by searching StackOverflow).
## Punctuation
* Errors should be written in sentence case, and should end in a full stop.
Bullets should be formatted similarly; make sure to capitalise the first
word (unless it's an argument or column name).
* Prefer the singular in problem statements:
```{r}
# GOOD
map_int(1:2, ~ "a")
#> Error: Each result must be coercible to a single integer:
#> * Result 1 is a character vector.
# BAD
map_int(1:2, ~ "a")
#> Error: Results must be coercible to single integers:
#> * Result 1 is a character vector
```
* If you can detect multiple problems, list up to five. This allows the user
to fix multiple problems in a single pass without being overwhelmed by
many errors that may have the same source.
```{r}
# BETTER
map_int(1:10, ~ "a")
#> Error: Each result must be coercible to a single integer:
#> * Result 1 is a character vector
#> * Result 2 is a character vector
#> * Result 3 is a character vector
#> * Result 4 is a character vector
#> * Result 5 is a character vector
#> * ... and 5 more problems
```
* Pick a natural connector between problem statement and error location:
this may be ", not", ";", or ":" depending on the context.
* Surround the names of arguments in backticks, e.g. `` `x` ``.
Use "column" to disambiguate columns and arguments: `` Column `x` ``.
Avoid "variable", because it is ambiguous.
* Ideally, each component of the error message should be less than 80
characters wide. Do not add manual line breaks to long error messages;
they will not look correct if the console is narrower (or much wider) than
expected. Instead, use bullets to break up the error into shorter logical
components.
## Before and after
More examples gathered from around the tidyverse.
```{r}
dplyr::filter(mtcars, cyl)
#> BEFORE: Argument 2 filter condition does not evaluate to a logical vector
#> AFTER: Each argument must be a logical vector:
#> * Argument 2 (`cyl`) is an integer vector.
tibble::tribble("x", "y")
#> BEFORE: Expected at least one column name; e.g. `~name`
#> AFTER: Must supply at least one column name, e.g. `~name`.
ggplot2::ggplot(data = diamonds) + ggplot2::geom_line(ggplot2::aes(x = cut))
#> BEFORE: geom_line requires the following missing aesthetics: y
#> AFTER: `geom_line()` must have the following aesthetics: `y`.
dplyr::rename(mtcars, cyl = xxx)
#> BEFORE: `xxx` contains unknown variables
#> AFTER: Can't find column `xxx` in `.data`.
dplyr::arrange(mtcars, xxx)
#> BEFORE: Evaluation error: object 'xxx' not found.
#> AFTER: Can't find column `xxx` in `.data`.
```