Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update default debug behaviour #976

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Added support for graphic devices provided by ragg and svglite (@thomasp85 #964)
* `parse_rds()`, `parse_feather()`, and `parse_parquet()` no longer writes data to disk during parsing (@thomasp85, #942)
* Returning error messages are now turned off by default rather than being turned on if running interactively and turned off if not (@thomasp85, #962)

# plumber 1.2.2

Expand Down
34 changes: 17 additions & 17 deletions R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Plumber <- R6Class(
#' Mac OS X, port numbers smaller than 1025 require root privileges.
#'
#' This value does not need to be explicitly assigned. To explicitly set it, see [options_plumber()].
#' @param debug If `TRUE`, it will provide more insight into your API errors. Using this value will only last for the duration of the run. If a `$setDebug()` has not been called, `debug` will default to `interactive()` at `$run()` time. See `$setDebug()` for more details.
#' @param debug If `TRUE`, it will provide more insight into your API errors. Using this value will only last for the duration of the run. If a `$setDebug()` has not been called, `debug` will default to `FALSE` at `$run()` time. See `$setDebug()` for more details.
#' @param swagger Deprecated. Please use `docs` instead. See `$setDocs(docs)` or `$setApiSpec()` for more customization.
#' @param swaggerCallback An optional single-argument function that is
#' called back with the URL to an OpenAPI user interface when one becomes
Expand Down Expand Up @@ -223,17 +223,16 @@ Plumber <- R6Class(
)
)

# Delay the setting of debug as long as possible.
# The router could be made in an interactive setting and used in background process.
# Do not determine if interactive until run time
# Temporarily set the debug value
prev_debug <- private$debug
on.exit({
private$debug <- prev_debug
}, add = TRUE)
# Fix the debug value while running.
# Determine if the user should be informed about default behavior
inform_debug <- is.null(prev_debug) && rlang::is_missing(debug)

# Set debug value, defaulting to already set value (which returns FALSE if not set)
self$setDebug(
# Order: Run method param, internally set value, is interactive()
# `$getDebug()` is dynamic given `setDebug()` has never been called.
rlang::maybe_missing(debug, self$getDebug())
)

Expand Down Expand Up @@ -267,6 +266,14 @@ Plumber <- R6Class(
on.exit(unmount_docs(self, docs_info), add = TRUE)
}

if (!isTRUE(quiet) && inform_debug && rlang::is_interactive()) {
rlang::inform(
"Error reporting has been turned off by default. See `pr_set_debug()` for more details.\nTo disable this message, set a debug value.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

.frequency="once",
.frequency_id="pr_set_debug_message"
)
}

on.exit(private$runHooks("exit"), add = TRUE)

httpuv::runServer(host, port, self)
Expand Down Expand Up @@ -963,15 +970,15 @@ Plumber <- R6Class(
#'
#' See also: `$getDebug()` and [pr_set_debug()]
#' @param debug `TRUE` provides more insight into your API errors.
setDebug = function(debug = interactive()) {
setDebug = function(debug = FALSE) {
stopifnot(length(debug) == 1)
private$debug <- isTRUE(debug)
},
#' @description Retrieve the `debug` value. If it has never been set, the result of `interactive()` will be used.
#' @description Retrieve the `debug` value. If it has never been set, it will return `FALSE`.
#'
#' See also: `$getDebug()` and [pr_set_debug()]
getDebug = function() {
private$debug %||% default_debug()
private$debug %||% FALSE
},
#' @description Add a filter to plumber router
#'
Expand Down Expand Up @@ -1333,13 +1340,6 @@ upgrade_docs_parameter <- function(docs, ...) {
)
}



default_debug <- function() {
interactive()
}


urlHost <- function(scheme = "http", host, port, path = "", changeHostLocation = FALSE) {
if (isTRUE(changeHostLocation)) {
# upgrade callback location to be localhost and not catch-all addresses
Expand Down
14 changes: 11 additions & 3 deletions R/pr_set.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ pr_set_error <- function(pr, fun) {

#' Set debug value to include error messages of routes cause an error
#'
#' To hide any error messages in production, set the debug value to `FALSE`.
#' The `debug` value is enabled by default for [interactive()] sessions.
#' By default, error messages from your plumber routes are hidden, but can be
#' turned on by setting the debug value to `TRUE` using this setter.
#'
#' @template param_pr
#' @param debug `TRUE` provides more insight into your API errors.
Expand All @@ -118,7 +118,15 @@ pr_set_error <- function(pr, fun) {
#' pr_get("/boom", function() stop("boom")) %>%
#' pr_run()
#' }
pr_set_debug <- function(pr, debug = interactive()) {
#'
#' # Setting within a plumber file
#' #* @plumber
#' function(pr) {
#' pr %>%
#' pr_set_debug(TRUE)
#' }
#'
pr_set_debug <- function(pr, debug = FALSE) {
validate_pr(pr)
pr$setDebug(debug = debug)
pr
Expand Down
6 changes: 3 additions & 3 deletions man/Plumber.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions man/pr_set_debug.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 0 additions & 38 deletions tests/testthat/test-plumber-run.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,41 +93,3 @@ test_that("`swaggerCallback` can be set by option after the pr is created", {
mockery::expect_called(m, 1)

})


### Test does not work as expected with R6 objects.
test_that("`debug` is not set until runtime", {
skip_if_not_installed("mockery", "0.4.2")

m <- mockery::mock(TRUE, cycle = TRUE)
local_mocked_bindings(default_debug = m)

root <- pr()
root$getDebug()
mockery::expect_called(m, 1)

with_interrupt({
root %>% pr_run(quiet = TRUE)
})
# increase by 1
mockery::expect_called(m, 2)

# listen to set value
with_interrupt({
root %>%
pr_set_debug(TRUE) %>%
pr_run(quiet = TRUE)
})
# not updated. stay at 2
mockery::expect_called(m, 2)

# listen to run value
with_interrupt({
root %>%
pr_run(debug = FALSE, quiet = TRUE)
})
# not updated. stay at 2
mockery::expect_called(m, 2)

# TODO test that run(debug=) has preference over pr_set_debug()
})
Loading