Skip to content

Commit

Permalink
Reformated error message when query fails.
Browse files Browse the repository at this point in the history
Note, qhql has changed how it handles errors and users
will get much more informative errors using the
current devel version (>=0.1.1.93).
  • Loading branch information
daynefiler committed Dec 8, 2021
1 parent 51f7517 commit eae66da
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
Imports:
crayon,
data.table,
ghql,
glue,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(print,failedQuery)
export(DatasetIds)
export(ReferenceGenomeIds)
export(apiUrl)
Expand All @@ -12,6 +13,7 @@ export(getVariantPopData)
export(getVariantsFromRegion)
export(validDatasets)
export(validGenomes)
import(crayon)
import(ghql)
importFrom(data.table,":=")
importFrom(data.table,.BY)
Expand Down
2 changes: 1 addition & 1 deletion R/convertVariantIds.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ convertVariantIds <- function(varids, genomes) {
}}
'
rsp <- .makeAndEvalQuery(qfmt, environment())
if (is(rsp, 'try-error')) return(rsp)
if (is(rsp, 'failedQuery')) return(rsp)
res <- fromJSON(rsp, flatten = TRUE)$data
res
}
Expand Down
2 changes: 1 addition & 1 deletion R/getVariantPopData.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ getVariantPopData <- function(varids, genomes) {
}}
'
rsp <- .makeAndEvalQuery(qfmt, environment())
if (is(rsp, 'try-error')) return(rsp)
if (is(rsp, 'failedQuery')) return(rsp)
resLst <- fromJSON(rsp, flatten = TRUE)$data
procPopData <- function(x) {
cbind(varid = x$variantId,
Expand Down
2 changes: 1 addition & 1 deletion R/getVariantsFromRegion.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ getVariantsFromRegion <- function(genomes, chroms, starts, stops = starts) {
}}
'
rsp <- .makeAndEvalQuery(qfmt, environment())
if (is(rsp, 'try-error')) return(rsp)
if (is(rsp, 'failedQuery')) return(rsp)
res <- lapply(fromJSON(rsp)$data, "[[", 1)
res
}
Expand Down
15 changes: 14 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ getLiftoverIdName <- function(genomes) {
.makeAndEvalQuery <- function(qfmt, genv, maxTries = 3) {
gmCon <- GraphqlClient$new(url = apiUrl())
qryBody <- glue_collapse(glue(qfmt, .envir = genv), sep = "\n")
qry <- Query$new()$query('q', glue('query {{ {qryBody} }}'))
qry <- Query$new()$query('q', glue('query {{\n{qryBody}\n}}'))
tries <- 1
repeat {
if (tries > maxTries) break
Expand All @@ -121,5 +121,18 @@ getLiftoverIdName <- function(genomes) {
Sys.sleep(2*tries)
tries <- tries + 1
}
if (is(tryres, 'try-error')) {
tryres <- list(errorMessage = tryres[1], query = qry$q$query)
class(tryres) <- "failedQuery"
}
tryres
}

#' @import crayon
#' @export

print.failedQuery <- function(x) {
cat("Query failed with the following message:\n" %+% red(x$errorMessage))
cat("Query string (stored as x$query):\n" %+% cyan(x$query))
}

0 comments on commit eae66da

Please sign in to comment.