Skip to content

Commit

Permalink
Add informative error message when connectionDetailsReference not set -
Browse files Browse the repository at this point in the history
fixes #100
  • Loading branch information
anthonysena committed Jan 11, 2024
1 parent 4e188ea commit 912c7c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions R/Settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ retrieveConnectionDetails <- function(connectionDetailsReference, keyringName =
checkmate::assertLogical(x = (is.null(keyringName) || keyringName %in% keyringList$keyring), add = errorMessages)
checkmate::reportAssertions(collection = errorMessages)

if (!connectionDetailsReference %in% keyring::key_list(keyring = keyringName)$service) {
stop("Connection details with connectionDetailsReference = \"", connectionDetailsReference, "\" were not found in your keyring. Please check that you have used the Strategus storeConnectionDetails function to save your connection details with this connectionDetailsReference name.")
}

# If the keyring is locked, unlock it, set the value and then re-lock it
keyringLocked <- unlockKeyring(keyringName = keyringName)

Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/test-Settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ test_that("Store and retrieve connection details", {
connFromKeyring <- DatabaseConnector::connect(
connectionDetailsFromKeyring
)
DatabaseConnector::disconnect(connFromKeyring)
expect_silent(DatabaseConnector::disconnect(connFromKeyring))
}
})

test_that("Retrieve connection details that do not exists throws informative error", {
# Setup keyring for the test
Sys.setenv("STRATEGUS_KEYRING_PASSWORD" = keyringPassword)
createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword)
on.exit(deleteKeyringForUnitTest())
expect_error(retrieveConnectionDetails(connectionDetailsReference = "does-not-exist", keyringName = keyringName))
})

0 comments on commit 912c7c1

Please sign in to comment.