-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_try.R
180 lines (171 loc) · 4.96 KB
/
http_try.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
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
#' Client/Server Error Handling
#'
#' Differentiate between client (4xx) and server (5xx) errors.
#' Provides a mechanism to return custom status codes
#' in combination with [`http_error()`] and [`http_success()`].
#'
#' @param req The request object.
#' @param res The response object.
#' @param expr An R expression to try.
#' @param x The return value from `try(expr)`.
#' @param silent Logical, should the report of error messages be suppressed by [try()]?
#' @param ... Arguments passed to [try()]
#'
#' @details
#' If we catch an error:
#'
#' * the error is a clear server error coming from `stop()`
#' * log it as an ERROR + print the error message to STDERR
#' * return a generic status 500 message
#' * set the status code of the response object to 500
#' * the error is a structured HTTP error coming from `http_error()`
#' * log it as an ERROR with the message from the condition attribute
#' * return the specific HTTP error code with the structured output
#' * set the status code of the response object
#'
#' If we don't catch an error:
#'
#' * the object is of class `http_success()` (this comes in handy for async jobs and redirects)
#' * log it as a SUCCESS with the message element
#' * return the specific HTTP status code with the structured output
#' * set the status code of the response object
#' * the object is NOT of class `http_success()`
#' * log it as a SUCCESS with a generic 200 message
#' * return the object as is (default status code 200 assumed)
#'
#' @return A list or the results from `expr`.
#' A side effect is setting of the response status code on the response
#' object and a log message to STDOUT or STDERR.
#'
#' @examples
#' req <- new.env()
#' res <- new.env()
#' http_try(req, res)
#' res$status
#'
#' req <- new.env()
#' res <- new.env()
#' http_try(req, res, { 2 + 2 })
#' res$status
#'
#' req <- new.env()
#' res <- new.env()
#' http_try(req, res, http_error(401))
#' res$status
#'
#' req <- new.env()
#' res <- new.env()
#' http_try(req, res, http_success(201))
#' res$status
#'
#' req <- new.env()
#' res <- new.env()
#' http_try(req, res, { lm(NULL) })
#' res$status
#'
#' req <- new.env()
#' res <- new.env()
#' http_try(req, res, { stop("Stop!!!") })
#' res$status
#'
#' req <- new.env()
#' res <- new.env()
#' f <- function() stop("Stop!!!")
#' http_try(req, res, { f() })
#' res$status
#'
#' req <- new.env()
#' res <- new.env()
#' http_try_handler(req, res, { try(f()) })
#' res$status
#'
#' @seealso [try()], [msg()]
#' @name http-try
NULL
#' @rdname http-try
#' @export
http_try <- function(req, res, expr, silent = TRUE, ...) {
http_try_handler(
req = req,
res = res,
x = try(
expr,
silent = silent,
...
)
)
}
#' @rdname http-try
#' @export
http_try_handler <- function(req, res, x) {
af <- api_framework(req, res)
if (inherits(x, "try-error")) {
if (!inherits(attr(x, "condition"), "http_error")) {
msg(
title = paste0(
"Status 500: ",
http_status_codes["500", "message"]),
message = one_line(geterrmessage()),
level = "ERROR"
)
# res$status <- 500L
if (af == "plumber" || is.na(af)) {
res$status <- 500L
}
if (!is.na(af) && af == "RestRserve") {
res$set_status_code(500L)
}
i <- as.list(
http_status_codes["500",]
)
i[] <- lapply(i, un_box)
i
} else {
msg(
title = paste0(
"Status ", attr(x, "condition")$status, ": ",
attr(x, "condition")$message),
level = "ERROR"
)
# res$status <- attr(x, "condition")$status
if (af == "plumber" || is.na(af)) {
res$status <- attr(x, "condition")$status
}
if (!is.na(af) && af == "RestRserve") {
res$set_status_code(attr(x, "condition")$status)
}
i <- unclass(
attr(x, "condition")
)
i[] <- lapply(i, un_box)
i
}
} else {
if (!inherits(x, "http_success")) {
msg(
title = paste0(
"Status 200: ",
http_status_codes["200", "message"]),
level = "SUCCESS"
)
x
} else {
msg(
title = paste0(
"Status ", x$status, ": ",
x$message),
level = "SUCCESS"
)
# res$status <- x$status
if (af == "plumber" || is.na(af)) {
res$status <- x$status
}
if (!is.na(af) && af == "RestRserve") {
res$set_status_code(x$status)
}
unclass(
x # no unboxing applied
)
}
}
}