Skip to content

Commit ae3d1b3

Browse files
committed
Improve error handling
1 parent 32341bb commit ae3d1b3

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

R/fetch_interpro.R

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,14 @@ fetch_interpro <- function(uniprot_ids = NULL,
109109
.f = ~ {
110110
# Initialize the next page URL with the base query URL
111111
next_url <- paste0("https://www.ebi.ac.uk/interpro/api/", .x, "&page_size=", page_size)
112-
attempts <- 0
113112
all_results <- list()
114113

115114
# progress bar variable
116115
total_pages <- 1
117116
current_page <- 1
118117

119118
while (!is.null(next_url)) {
120-
result <- tryCatch(
121-
{
119+
122120
# Call the protti function to query the API
123121
query <- protti:::try_query(next_url,
124122
type = "application/json",
@@ -129,44 +127,29 @@ fetch_interpro <- function(uniprot_ids = NULL,
129127
# Store the results from the current page
130128
all_results[[current_page]] <- query
131129

130+
132131
# initialize progress bar
133-
if (show_progress == TRUE & current_page == 1 & length(query_url_part) == 1 & !is.null(query$count)) {
132+
if (show_progress == TRUE & current_page == 1 & length(query_url_part) == 1 & (!is(query, "character") && !is.null(query$count))) {
134133
pb <- progress::progress_bar$new(
135134
total = ceiling(query$count / page_size),
136135
format = " Fetching Pages [:bar] :current/:total (:percent) :eta"
137136
)
138137
}
139138

140139
# tick progress bar
141-
if (show_progress == TRUE & length(query_url_part) == 1 & !is.null(query$count) && !pb$finished) {
140+
if (show_progress == TRUE & length(query_url_part) == 1 & (!is(query, "character") && !is.null(query$count)) && !pb$finished) {
142141
pb$tick()
143142
}
144143

145-
# Get the next page URL from the "next" field
146-
next_url <- query$`next`
144+
if (!is(query, "character")) {
145+
# Get the next page URL from the "next" field
146+
next_url <- query$`next`
147+
} else {
148+
next_url <- NULL
149+
}
147150

148151
# add to page counter
149152
current_page <- current_page + 1
150-
151-
attempts <- 0 # Reset attempts counter on successful request
152-
153-
NULL
154-
},
155-
error = function(e) {
156-
if (grepl("408", e$message)) {
157-
Sys.sleep(2) # Sleep and retry for timeout errors (408)
158-
NULL
159-
} else {
160-
attempts <<- attempts + 1
161-
if (attempts < max_tries) {
162-
Sys.sleep(2) # Sleep and retry up to "max_tries" times
163-
NULL
164-
} else {
165-
stop(paste("Error retrieving data for URL:", next_url, " - ", e$message))
166-
}
167-
}
168-
}
169-
)
170153
}
171154
all_results
172155
},
@@ -175,10 +158,31 @@ fetch_interpro <- function(uniprot_ids = NULL,
175158
}
176159

177160
if (!missing(manual_query)) {
178-
# return raw data for manual queries
179-
return(query_result)
161+
# Check if any element is of type character to report issues with the fetching to the user.
162+
if(any(purrr::map_lgl(query_result[[1]], is.character))) {
163+
164+
issue <- purrr::keep(query_result[[1]], is.character)[[1]]
165+
166+
message("\nThere was an issue fetching from InterPro:\n", issue)
167+
return(NULL)
168+
} else {
169+
# return raw data for manual queries
170+
return(query_result)
171+
}
172+
}
173+
174+
# If any element is character stop the function and report the issue
175+
if (exists("query_result") && any(purrr::map_lgl(query_result, .f = ~ {purrr::map_lgl(.x, is.character)}))) {
176+
177+
problematic_ids <- uniprot_ids[purrr::map_lgl(query_result, .f = ~ {any(purrr::map_lgl(.x, is.character))})]
178+
issue <- purrr::keep(query_result, .p = ~ {purrr::map_lgl(.x, is.character)}) %>%
179+
unlist()
180+
181+
message("\nThere was an issue fetching from InterPro:\n", paste0(problematic_ids, ": ", issue, "\n"))
182+
return(NULL)
180183
}
181184

185+
182186
# if residue level information should be returned, fetch it here
183187
if (return_residue_info) {
184188
query_residue <- purrr::map_dfr(
@@ -193,6 +197,10 @@ fetch_interpro <- function(uniprot_ids = NULL,
193197
timeout = timeout
194198
)
195199

200+
if (is(query, "character")){
201+
result <- data.frame(accession = .x,
202+
issue = query)
203+
} else {
196204
if (length(query) == 0) {
197205
result <- data.frame(accession = .x)
198206
} else {
@@ -231,12 +239,21 @@ fetch_interpro <- function(uniprot_ids = NULL,
231239
) %>%
232240
mutate(accession = .x)
233241
}
242+
}
234243

235244
result
236245
},
237246
.progress = progress_option_residue
238247
)
239248

249+
if("issue" %in% colnames(query_residue)){
250+
problematic_ids <- query_residue %>%
251+
dplyr::filter(!is.na(issue))
252+
253+
message("\nThere was an issue fetching from InterPro:\n", paste0(problematic_ids$accession, ": ", problematic_ids$issue, "\n"))
254+
return(NULL)
255+
}
256+
240257
return(query_residue)
241258
}
242259

0 commit comments

Comments
 (0)