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

Improve error messages #30

Merged
merged 9 commits into from
Oct 11, 2024
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@

* Define a current user and current instance with lamin-cli prior to testing and generating documentation in the CI (PR #23).

## TESTING

* Add a simple unit test which queries laminlabs/lamindata (PR #27).

* Added unit test for the InstanceAPI class (PR #30).

## DOCUMENTATION

* Update `README` with new set up instructions and simplify (PR #14).
Expand All @@ -49,6 +53,8 @@

* Return `NULL` when a record's related field is empty (PR #28).

* Add alternative error message when no message is returned from the API (PR #30).

# laminr v0.0.1

Initial POC implementation of the LaminDB API client for R.
Expand Down
40 changes: 21 additions & 19 deletions R/InstanceAPI.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter
#' Get the schema for the instance.
get_schema = function() {
# TODO: replace with laminr.api get_schema call
request <- httr::GET(
paste0(
private$.instance_settings$api_url,
"/instances/",
private$.instance_settings$id,
"/schema"
)
url <- paste0(
private$.instance_settings$api_url,
"/instances/",
private$.instance_settings$id,
"/schema"
)

content <- httr::content(request)
if (httr::http_error(request)) {
cli_abort(content$detail)
}
response <- httr::GET(url)

content
private$process_response(response, "get schema")
},
#' Get a record from the instance.
#' @importFrom jsonlite toJSON
Expand Down Expand Up @@ -87,7 +82,7 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter
tolower(include_foreign_keys)
)

request <- httr::POST(
response <- httr::POST(
url,
httr::add_headers(
accept = "application/json",
Expand All @@ -96,15 +91,22 @@ InstanceAPI <- R6::R6Class( # nolint object_name_linter
body = body
)

content <- httr::content(request)
if (httr::http_error(request)) {
cli_abort(content$detail)
private$process_response(response, "get record")
}
),
private = list(
.instance_settings = NULL,
process_response = function(response, request_type) {
content <- httr::content(response)
if (httr::http_error(response)) {
if (is.list(content) && "detail" %in% names(content)) {
cli_abort(content$detail)
} else {
cli_abort("Failed to {request_type} from instance. Output: {content}")
}
}

content
}
),
private = list(
.instance_settings = NULL
)
)
12 changes: 6 additions & 6 deletions R/InstanceSettings.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ InstanceSettings <- R6::R6Class( # nolint object_name_linter
"db_user_password", # api
"lamindb_version" # api
)
missing_column <- setdiff(expected_keys, names(settings))
if (length(missing_column) > 0) {
cli_abort("Missing column: ", missing_column)
missing_keys <- setdiff(expected_keys, names(settings))
if (length(missing_keys) > 0) {
cli_abort("Missing key{?s}: {missing_keys}")
}
unexpected_columns <- setdiff(names(settings), c(expected_keys, optional_keys))
if (length(unexpected_columns) > 0) {
cli_abort("Unexpected column: ", unexpected_columns)
unexpected_keys <- setdiff(names(settings), c(expected_keys, optional_keys))
if (length(unexpected_keys) > 0) {
cli_abort("Unexpected key{?s}: {unexpected_keys}")
}
private$.settings <- settings
}
Expand Down
105 changes: 105 additions & 0 deletions tests/testthat/test-instance_api.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
skip_if_offline()

broken_instance_settings <- function() {
InstanceSettings$new(
list(
owner = "foo",
name = "bar",
id = "...",
schema_str = "foo,bar",
schema_id = "...",
git_repo = "...",
keep_artifacts_local = TRUE,
api_url = "https://foo.lamin.ai"
)
)
}

test_that("get_schema works", {
local_setup_lamindata_instance()

instance_file <- .settings_store__instance_settings_file("laminlabs", "lamindata")
instance_settings <- .settings_load__load_instance_settings()

api <- InstanceAPI$new(instance_settings)

# try to get the schema
schema <- api$get_schema()

expect_named(schema, c("core", "bionty", "wetlab"))

expect_true(all(c("run", "user", "param", "artifact", "storage") %in% names(schema$core)))

expect_named(schema$core$artifact, c("fields_metadata", "class_name", "is_link_table"))
})

test_that("get_schema fails gracefully", {
instance_settings <- broken_instance_settings()

api <- InstanceAPI$new(instance_settings)

expect_error(api$get_schema(), regexp = "Could not resolve host: foo.lamin.ai")
})

test_that("get_record works", {
local_setup_lamindata_instance()

instance_file <- .settings_store__instance_settings_file("laminlabs", "lamindata")
instance_settings <- .settings_load__load_instance_settings()

api <- InstanceAPI$new(instance_settings)

# try to get a record
artifact <- api$get_record("core", "artifact", "mePviem4DGM4SFzvLXf3")

expect_true(all(c("uid", "size", "hash", "description", "type") %in% names(artifact)))
})

test_that("test get_record fails gracefully with incorrect host", {
instance_settings <- broken_instance_settings()

api <- InstanceAPI$new(instance_settings)

# try to get a record
expect_error(
api$get_record("core", "artifact", "mePviem4DGM4SFzvLXf3"),
regexp = "Could not resolve host: foo.lamin.ai"
)
})

test_that("get_record with select works", {
local_setup_lamindata_instance()

instance_file <- .settings_store__instance_settings_file("laminlabs", "lamindata")
instance_settings <- .settings_load__load_instance_settings()

api <- InstanceAPI$new(instance_settings)

# try to get a record
artifact <- api$get_record("core", "artifact", "mePviem4DGM4SFzvLXf3", select = "storage")

expect_true(all(c("uid", "size", "hash", "description", "type") %in% names(artifact)))

expect_true(all(c("uid", "type", "region", "root") %in% names(artifact$storage)))
})

test_that("get_record fails gracefully", {
local_setup_lamindata_instance()

instance_file <- .settings_store__instance_settings_file("laminlabs", "lamindata")
instance_settings <- .settings_load__load_instance_settings()

api <- InstanceAPI$new(instance_settings)

# nolint start: commented_code
# TODO: improve error messages for these cases
expect_error(
api$get_record("core", "artifact", "foobar")#,
# regexp = "Error getting record: list index out of range"
)
expect_error(
api$get_record("core", "artifact", "mePviem4DGM4SFzvLXf3", select = "foo"),
# regexp = "Error getting record: invalid select field: foo"
)
# nolint end: commented_code
})
Loading