Skip to content

Commit 2942ae2

Browse files
committed
Better handling of errors that aren't JSON
1 parent 33241d8 commit 2942ae2

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 1.0.5.9000
22

3+
* Better display of error messages.
34

45
# 1.0.5
56

R/errors.R

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ report_error <- function(response) {
66
invisible(response)
77
} else {
88
call <- sys.call(-1)
9-
stop(create_condition(response, "error", call = call))
9+
10+
cond <- create_condition(response, "error", call = call)
11+
12+
# Sometimes the message from create_condition can be very long, but it is
13+
# necessary to show the whole thing so that the user can understand it.
14+
old_options <- options(warning.length = nchar(cond$message))
15+
on.exit(options(old_options))
16+
17+
stop(cond)
1018
}
1119
}
1220

@@ -18,24 +26,34 @@ create_condition <- function(response,
1826

1927
class <- match.arg(class)
2028

21-
cont <- content(response)
29+
cont <- content(response, as = "text")
30+
31+
message <- NULL
32+
status <- NULL
2233

23-
if (is.character(cont)) {
24-
# In rare cases the error content is just a string. This can happen, for
25-
# example, when there is a problem loading execute_script.js.
26-
# https://github.com/rstudio/shinytest/issues/165
34+
if (jsonlite::validate(cont)) {
35+
try({
36+
# This can error if `cont` doesn't include the fields we want.
37+
json <- fromJSON(
38+
cont[["value"]][["message"]],
39+
simplifyVector = FALSE
40+
)
41+
message <- json[["errorMessage"]] %||% "WebDriver error"
42+
status <- cont$status
43+
})
44+
}
45+
46+
# We can end up in this block if:
47+
# * The error content is just a string, or raw HTML. This can happen, for
48+
# example, when there is a problem loading execute_script.js.
49+
# https://github.com/rstudio/shinytest/issues/165
50+
# https://github.com/rstudio/shinytest/issues/190
51+
# * The `cont` object was JSON, but did not include the needed fields.
52+
if (is.null(status)) {
2753
message <- cont
2854
# Need to manually set status code for UnknownError. From:
2955
# https://github.com/detro/ghostdriver/blob/873c9d6/src/errors.js#L135
3056
status <- 13L
31-
32-
} else {
33-
json <- fromJSON(
34-
cont[["value"]][["message"]],
35-
simplifyVector = FALSE
36-
)
37-
message <- json[["errorMessage"]] %||% "WebDriver error"
38-
status <- cont$status
3957
}
4058

4159

0 commit comments

Comments
 (0)