Skip to content

Commit

Permalink
Add special handling for httr2_oauth_parse errors.
Browse files Browse the repository at this point in the history
Picks up on the special error class introduced in r-lib/httr2#596.

This substantially improves the error message when there are
misconfigurations on the Connect side.

This commit does not update the snapshots so that tests continue to pass
with the CRAN version of httr2.

Signed-off-by: Aaron Jacobs <aaron.jacobs@posit.co>
  • Loading branch information
atheriel committed Dec 12, 2024
1 parent e925be3 commit 9a07d3e
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions R/viewer-based-credentials.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ connect_viewer_token <- function(session,
# Don't use the cached token when testing.
reauth = is_testing()
),
httr2_oauth_parse = function(cnd) {
# The original implementation of viewer-based credentials returned a
# regular Connect error response, which httr2 doesn't understand. Try to
# turn it into a useful message in that case.
body <- try(resp_body_json(cnd$resp), TRUE)
if (inherits(body, "try-error") || !has_name(body, "error_message")) {
# Re-throw errors we don't expect.
stop(cnd)
}
# Emulate httr2:::oauth_flow_abort().
cli::cli_abort(c(
"OAuth failure [invalid_request]",
"*" = body$error_message,
i = if (body$error_code == 212) "Learn more at \
{.url https://docs.posit.co/connect/user/oauth-integrations/#adding-oauth-integrations-to-deployed-content}."
), call = NULL)
},
error = function(cnd) {
cli::cli_abort(
"Cannot fetch viewer-based credentials for the current Shiny session.",
Expand Down

0 comments on commit 9a07d3e

Please sign in to comment.