Skip to content

Commit c74adbe

Browse files
committed
Fix #5 add more info on conditions
1 parent fb2d676 commit c74adbe

File tree

10 files changed

+136
-4
lines changed

10 files changed

+136
-4
lines changed

NAMESPACE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ S3method(roxy_tag_parse,roxy_tag_trace)
3939
export(Break)
4040
export(Next)
4141
export(Plumber)
42+
export(abort_bad_request)
43+
export(abort_conflict)
44+
export(abort_forbidden)
45+
export(abort_gone)
46+
export(abort_http_problem)
47+
export(abort_internal_error)
48+
export(abort_method_not_allowed)
49+
export(abort_not_acceptable)
50+
export(abort_not_found)
51+
export(abort_status)
52+
export(abort_unauthorized)
4253
export(api)
4354
export(api_add_route)
4455
export(api_any)
@@ -106,6 +117,7 @@ export(parse_rds)
106117
export(parse_text)
107118
export(parse_tsv)
108119
export(parse_yaml)
120+
export(random_key)
109121
export(register_parser)
110122
export(register_serializer)
111123
export(show_registered_parsers)
@@ -118,6 +130,18 @@ importFrom(jsonlite,write_json)
118130
importFrom(ragg,agg_jpeg)
119131
importFrom(ragg,agg_png)
120132
importFrom(ragg,agg_tiff)
133+
importFrom(reqres,abort_bad_request)
134+
importFrom(reqres,abort_conflict)
135+
importFrom(reqres,abort_forbidden)
136+
importFrom(reqres,abort_gone)
137+
importFrom(reqres,abort_http_problem)
138+
importFrom(reqres,abort_internal_error)
139+
importFrom(reqres,abort_method_not_allowed)
140+
importFrom(reqres,abort_not_acceptable)
141+
importFrom(reqres,abort_not_found)
142+
importFrom(reqres,abort_status)
143+
importFrom(reqres,abort_unauthorized)
144+
importFrom(reqres,random_key)
121145
importFrom(routr,Route)
122146
importFrom(routr,RouteStack)
123147
importFrom(routr,openapi_route)

R/api.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ is_plumber_api <- function(x) inherits(x, "Plumber")
129129
api_parse <- function(api, ...) {
130130
locations <- dots_to_plumber_files(..., prefer_yml = FALSE)
131131
for (loc in locations) {
132-
api$parse_file(file)
132+
api$parse_file(loc)
133133
}
134134
api
135135
}

R/api_handlers.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,27 @@ handle_constructor <- function(method, header = FALSE) {
180180
#' be set to the response body (overwritting what was already there) and
181181
#' handling is then allowed to continue
182182
#'
183+
#' ## Handler conditions
184+
#' Like any function in R, a handler may need to signal that something happened,
185+
#' either by throwing an error or warning or by emitting a message. You can use
186+
#' [stop()], [warning()], and [message()] as you are used to. For all of them,
187+
#' the condition message will end up in the log. Further, for `stop()` any
188+
#' further handling of the request will end and a `500 Internal Error` response
189+
#' is returned. To take more control over problems you can use the
190+
#' [`abort_*()`][abort_status] family of conditions from reqres. Like `stop()`
191+
#' they will halt any further processing, but they also allow control over what
192+
#' kind of response is sent back, what kind of information about the issue is
193+
#' communicated to the client, and what kind of information is logged
194+
#' internally. The response they send back (except for `abort_status()`) all
195+
#' adhere to the HTTP Problem spec defined in
196+
#' [RFC 9457](https://datatracker.ietf.org/doc/html/rfc9457).
197+
#'
198+
#' While it may feel like a good idea to send a detailed error message back to
199+
#' the client it is often better to only inform the client of what they need to
200+
#' change to solve the issue. Too much information about internal implementation
201+
#' details can be a security risk and forwarding internal errors to a client can
202+
#' help inform the client about how the server has been implemented.
203+
#'
183204
#' @param api A plumber2 api object to add the handler to
184205
#' @param path A string giving the path the handler responds to. See Details
185206
#' @param handler A handler function to call when a request is matched to the

R/api_session_cookie.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ api_session_cookie <- function(
4040
)
4141
api
4242
}
43+
44+
#' @export
45+
#' @importFrom reqres random_key
46+
reqres::random_key

R/conditions.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#' @export
2+
#' @importFrom reqres abort_status
3+
reqres::abort_status
4+
#' @export
5+
#' @importFrom reqres abort_http_problem
6+
reqres::abort_http_problem
7+
#' @export
8+
#' @importFrom reqres abort_bad_request
9+
reqres::abort_bad_request
10+
#' @export
11+
#' @importFrom reqres abort_conflict
12+
reqres::abort_conflict
13+
#' @export
14+
#' @importFrom reqres abort_forbidden
15+
reqres::abort_forbidden
16+
#' @export
17+
#' @importFrom reqres abort_gone
18+
reqres::abort_gone
19+
#' @export
20+
#' @importFrom reqres abort_internal_error
21+
reqres::abort_internal_error
22+
#' @export
23+
#' @importFrom reqres abort_method_not_allowed
24+
reqres::abort_method_not_allowed
25+
#' @export
26+
#' @importFrom reqres abort_not_acceptable
27+
reqres::abort_not_acceptable
28+
#' @export
29+
#' @importFrom reqres abort_not_found
30+
reqres::abort_not_found
31+
#' @export
32+
#' @importFrom reqres abort_unauthorized
33+
reqres::abort_unauthorized

man/api_request_handlers.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/reexports.Rd

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/files/apis/04-05-error.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function() {
77
#* Generate a friendly error
88
#* @get /friendly
99
function() {
10-
reqres::abort_bad_request(
10+
abort_bad_request(
1111
"Your request could not be parsed"
1212
)
1313
}

vignettes/files/apis/07-02-plot-safe.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#* @serializer png
66
function(query) {
77
if (query$pts > 1000 & query$pts < 1){
8-
reqres::abort_bad_request("pts must be between 1 and 1,000")
8+
abort_bad_request("pts must be between 1 and 1,000")
99
}
1010
plot(1:pts)
1111
}

vignettes/files/examples/github.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function(request) {
3030
hm <- digest::hmac(secret, request$body, algo="sha1")
3131
hm <- paste0("sha1=", hm)
3232
if (!identical(hm, request$HTTP_X_HUB_SIGNATURE)) {
33-
reqres::abort_bad_request("invalid GitHub signature.")
33+
abort_bad_request("invalid GitHub signature.")
3434
}
3535

3636
# Install new package

0 commit comments

Comments
 (0)