Skip to content

Commit 328c7d0

Browse files
authored
Merge pull request #223 from CHOP-CGTInformatics/fix-dag-export-bug
Fix DAG export bug
2 parents 1a6ddf8 + 6426e25 commit 328c7d0

File tree

7 files changed

+71
-21
lines changed

7 files changed

+71
-21
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
REDCAPTIDIER_LONGITUDINAL_DAG_API: ${{ secrets.REDCAPTIDIER_LONGITUDINAL_DAG_API }}
3232
REDCAPTIDIER_MIXED_STRUCTURE_API: ${{ secrets.REDCAPTIDIER_MIXED_STRUCTURE_API }}
3333
REDCAPTIDIER_MDC_API: ${{ secrets.REDCAPTIDIER_MDC_API }}
34+
REDCAPTIDIER_DAG_ACCESS_API: ${{ secrets.REDCAPTIDIER_DAG_ACCESS_API }}
3435
steps:
3536
- name: Update Ubuntu, Install cURL Headers, add Libraries
3637
run: |

.github/workflows/test-coverage.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
REDCAPTIDIER_DAG_API: ${{ secrets.REDCAPTIDIER_DAG_API }}
3131
REDCAPTIDIER_LONGITUDINAL_DAG_API: ${{ secrets.REDCAPTIDIER_LONGITUDINAL_DAG_API }}
3232
REDCAPTIDIER_MDC_API: ${{ secrets.REDCAPTIDIER_MDC_API }}
33+
REDCAPTIDIER_DAG_ACCESS_API: ${{ secrets.REDCAPTIDIER_DAG_ACCESS_API }}
3334
steps:
3435
- name: Update Ubuntu, Install cURL Headers, add Libraries
3536
run: |

R/read_redcap.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,13 @@ read_redcap <- function(redcap_uri,
204204
# If DAGs detected and requested in label format, trigger an API call and
205205
# update column data for redcap_data_access_group
206206
if ("redcap_data_access_group" %in% names(db_data) && raw_or_label != "raw") {
207-
dag_data <- redcap_dag_read(
208-
redcap_uri = redcap_uri,
209-
token = token,
210-
verbose = !suppress_redcapr_messages
211-
)$data
207+
dag_data <- try_redcapr({
208+
redcap_dag_read(
209+
redcap_uri = redcap_uri,
210+
token = token,
211+
verbose = !suppress_redcapr_messages
212+
)
213+
})
212214

213215
db_data <- update_dag_cols(
214216
data = db_data,

R/utils.R

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -805,19 +805,22 @@ try_redcapr <- function(expr, call = caller_env()) {
805805
"i" = "URI: `{condition$redcap_uri}`"
806806
)
807807
condition$class <- c("cannot_post", condition$class)
808+
} else if (!is.null(out$outcome_message) && str_detect(
809+
out$outcome_message,
810+
"You must have 'API Export' privileges and 'Data Access Groups' privileges"
811+
)
812+
) {
813+
condition$info <- c(
814+
"!" = "You do not have sufficient privileges to export data access groups.",
815+
"i" = "Set `export_data_access_groups = FALSE` if you do not intend to export data access groups."
816+
)
817+
condition$class <- c("dag_access_error", condition$class)
818+
condition$parent <- append_outcome_message(quo, out$outcome_message)
808819
} else {
809820
condition$class <- c("unexpected_error", condition$class)
810821

811822
if (!is.null(out$outcome_message)) {
812-
# Throw error containing outcome message and attach that as the parent
813-
# Get the name of the function called inside try_redcapr so it can be mentioned in the error message
814-
calling_fn <- quo_get_expr(quo)
815-
# Handle case where try_redcapr had multiline expr
816-
if (inherits(calling_fn, "{")) {
817-
calling_fn <- calling_fn[[2]]
818-
}
819-
820-
condition$parent <- catch_cnd(abort(out$outcome_message, call = calling_fn))
823+
condition$parent <- append_outcome_message(quo, out$outcome_message)
821824
}
822825
}
823826
cli_abort(
@@ -834,6 +837,18 @@ try_redcapr <- function(expr, call = caller_env()) {
834837
out$data
835838
}
836839

840+
append_outcome_message <- function(quo, outcome_message) {
841+
# Throw error containing outcome message and attach that as the parent
842+
# Get the name of the function called inside try_redcapr so it can be mentioned in the error message
843+
calling_fn <- quo_get_expr(quo)
844+
# Handle case where try_redcapr had multiline expr
845+
if (inherits(calling_fn, "{")) {
846+
calling_fn <- calling_fn[[2]]
847+
}
848+
849+
catch_cnd(abort(outcome_message, call = calling_fn))
850+
}
851+
837852

838853
#' @title
839854
#' Check whether a REDCap database has repeat forms

tests/testthat/test-read_redcap.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,3 +755,18 @@ test_that("update_dag_cols() works for haven labels", {
755755
expect_equal(out$redcap_data_access_group, expected_vec)
756756
expect_equal(out, expected_out)
757757
})
758+
759+
test_that("read_redcap() handles insufficient DAG access", {
760+
read_redcap(
761+
Sys.getenv("REDCAP_URI"),
762+
Sys.getenv("REDCAPTIDIER_DAG_ACCESS_API")
763+
) |>
764+
expect_error(class = "dag_access_error")
765+
766+
read_redcap(
767+
Sys.getenv("REDCAP_URI"),
768+
Sys.getenv("REDCAPTIDIER_DAG_ACCESS_API"),
769+
export_data_access_groups = FALSE
770+
) |>
771+
expect_no_error()
772+
})

utility/cli_message_examples.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,7 @@ read_redcap(Sys.getenv("REDCAP_URI"), Sys.getenv("REDCAPTIDIER_CLASSIC_API")) %>
151151
# missing data codes
152152

153153
read_redcap(redcap_uri, Sys.getenv("REDCAPTIDIER_MDC_API"))
154+
155+
# No DAG export access
156+
157+
read_redcap(Sys.getenv("REDCAP_URI"), Sys.getenv("REDCAPTIDIER_DAG_ACCESS_API"))

utility/cli_message_examples_reprex.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ read_redcap(redcap_uri, classic_token, export_survey_fields = 123)
112112

113113
read_redcap(redcap_uri, classic_token, export_survey_fields = c(TRUE, TRUE))
114114
#> Error in `read_redcap()`:
115-
#> ✖ You've supplied `TRUE` and `TRUE` for `export_survey_fields` which is
116-
#> not a valid value
115+
#> ✖ You've supplied `TRUE` `TRUE` for `export_survey_fields` which is not
116+
#> a valid value
117117
#> ! Must have length 1, but has length 2
118118

119119
## suppress_redcapr_messages
@@ -126,8 +126,8 @@ read_redcap(redcap_uri, classic_token, suppress_redcapr_messages = 123)
126126

127127
read_redcap(redcap_uri, classic_token, suppress_redcapr_messages = c(TRUE, TRUE))
128128
#> Error in `read_redcap()`:
129-
#> ✖ You've supplied `TRUE` and `TRUE` for `suppress_redcapr_messages`
130-
#> which is not a valid value
129+
#> ✖ You've supplied `TRUE` `TRUE` for `suppress_redcapr_messages` which is
130+
#> not a valid value
131131
#> ! Must have length 1, but has length 2
132132

133133
# data access groups
@@ -255,7 +255,7 @@ withr::with_tempdir({
255255
})
256256
#> Error:
257257
#> ✖ File
258-
#> ''/private/var/folders/qc/mmjjyjq50530z9r_7mfqcqfhxkkk67/T/Rtmph4LQ3Z/filee5aa7b9bea48/temp.csv''
258+
#> ''/private/var/folders/9c/k1m0bzys7gb1v32g86hfn5sn5k86h1/T/RtmpQCmzPP/file1411b189a9c52/temp.csv''
259259
#> already exists.
260260
#> ℹ Overwriting files is disabled by default. Set `overwrite = TRUE` to overwrite
261261
#> existing file.
@@ -296,7 +296,7 @@ withr::with_tempdir({
296296
write_redcap_xlsx(file = filepath)
297297
})
298298
#> Warning in write_redcap_xlsx(., file = filepath): ! No extension provided for `file`:
299-
#> '/private/var/folders/qc/mmjjyjq50530z9r_7mfqcqfhxkkk67/T/Rtmph4LQ3Z/filee5aa11455c98/temp'
299+
#> '/private/var/folders/9c/k1m0bzys7gb1v32g86hfn5sn5k86h1/T/RtmpQCmzPP/file1411b1319086f/temp'
300300
#> ℹ The extension '.xlsx' will be appended to the file name.
301301

302302
# Printed supertibble
@@ -338,6 +338,18 @@ read_redcap(redcap_uri, Sys.getenv("REDCAPTIDIER_MDC_API"))
338338
#> 1 form_1 Form 1 <tibble [3 × 9]> <tibble> nonrepeat…
339339
#> # ℹ 5 more variables: data_rows <int>, data_cols <int>, data_size <lbstr_by>,
340340
#> # data_na_pct <formttbl>, form_complete_pct <formttbl>
341+
342+
# No DAG export access
343+
344+
read_redcap(Sys.getenv("REDCAP_URI"), Sys.getenv("REDCAPTIDIER_DAG_ACCESS_API"))
345+
#> Error in `read_redcap()`:
346+
#> ✖ The REDCapR export operation was not successful.
347+
#> ! You do not have sufficient privileges to export data access groups.
348+
#> ℹ Set `export_data_access_groups = FALSE` if you do not intend to export data
349+
#> access groups.
350+
#> Caused by error in `redcap_dag_read()`:
351+
#> ! The REDCapR read/export operation was not successful. The error message was:
352+
#> ERROR: Insufficient user privileges: You must have 'API Export' privileges and 'Data Access Groups' privileges in the project.
341353
```
342354

343-
<sup>Created on 2024-10-16 with [reprex v2.1.0](https://reprex.tidyverse.org)</sup>
355+
<sup>Created on 2025-05-30 with [reprex v2.1.0](https://reprex.tidyverse.org)</sup>

0 commit comments

Comments
 (0)