Skip to content
Merged
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: 4 additions & 2 deletions R/utils_getSubnetworkFromIndra.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
}
if (filter_by_curation) {
for (i in seq_along(res)) {
stmt_hash <- res[[i]]$data$stmt_hash
stmt_json <- fromJSON(res[[i]]$data$stmt_json)
stmt_hash <- stmt_json$matches_hash
Comment on lines +128 to +129
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add error handling for JSON parsing and field validation.

The fromJSON call lacks error handling and there's no validation that matches_hash exists in the parsed JSON. If the INDRA API returns malformed JSON or if the structure changes, this will cause runtime failures.

Apply this diff to add defensive error handling:

         for (i in seq_along(res)) {
-            stmt_json <- fromJSON(res[[i]]$data$stmt_json)
-            stmt_hash <- stmt_json$matches_hash
+            stmt_json <- tryCatch(
+                fromJSON(res[[i]]$data$stmt_json),
+                error = function(e) {
+                    warning(paste("Failed to parse stmt_json for statement", i, ":", e$message))
+                    return(NULL)
+                }
+            )
+            if (is.null(stmt_json) || is.null(stmt_json$matches_hash)) {
+                warning(paste("Missing matches_hash for statement", i))
+                next
+            }
+            stmt_hash <- stmt_json$matches_hash
             incorrect_count <- .get_incorrect_curation_count(stmt_hash, api_key)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
stmt_json <- fromJSON(res[[i]]$data$stmt_json)
stmt_hash <- stmt_json$matches_hash
stmt_json <- tryCatch(
fromJSON(res[[i]]$data$stmt_json),
error = function(e) {
warning(paste("Failed to parse stmt_json for statement", i, ":", e$message))
return(NULL)
}
)
if (is.null(stmt_json) || is.null(stmt_json$matches_hash)) {
warning(paste("Missing matches_hash for statement", i))
next
}
stmt_hash <- stmt_json$matches_hash
🤖 Prompt for AI Agents
In R/utils_getSubnetworkFromIndra.R around lines 128-129, the call stmt_json <-
fromJSON(res[[i]]$data$stmt_json) and immediate access to stmt_json$matches_hash
assume valid JSON and presence of matches_hash; add defensive error handling:
wrap fromJSON in tryCatch to catch/parsing errors (log/warn and skip or set
stmt_hash to NA), validate that parsed object is a list and contains the
matches_hash field before accessing it, handle missing or NULL matches_hash
(log/warn and skip or set NA), and ensure subsequent code branches handle
NA/absence safely so malformed or changed INDRA responses do not cause runtime
failures.

incorrect_count <- .get_incorrect_curation_count(stmt_hash, api_key)
res[[i]]$data$evidence_count <- res[[i]]$data$evidence_count - incorrect_count
# Todo: Also subtract source_counts accordingly if requested
Expand Down Expand Up @@ -301,7 +302,8 @@
query(res, x)$data$source_counts
}, ""),
stmt_hash = vapply(keys(res), function(x) {
as.character(query(res, x)$data$stmt_hash)
stmt_json <- fromJSON(query(res, x)$data$stmt_json)
stmt_json$matches_hash
}, ""),
Comment on lines 304 to 307
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add error handling and validation for JSON parsing in vapply.

Similar to the issue in .filterIndraResponse, this fromJSON call lacks error handling. Additionally, since this is inside a vapply expecting character output, any parsing failure will cause a type mismatch error that's harder to debug.

Apply this diff to add defensive error handling:

         stmt_hash = vapply(keys(res), function(x) {
-            stmt_json <- fromJSON(query(res, x)$data$stmt_json)
-            stmt_json$matches_hash
+            tryCatch({
+                stmt_json <- fromJSON(query(res, x)$data$stmt_json)
+                if (is.null(stmt_json$matches_hash)) {
+                    return(NA_character_)
+                }
+                stmt_json$matches_hash
+            }, error = function(e) {
+                warning(paste("Failed to parse stmt_json for key", x, ":", e$message))
+                return(NA_character_)
+            })
         }, ""),
🤖 Prompt for AI Agents
In R/utils_getSubnetworkFromIndra.R around lines 304 to 307, the inline
fromJSON(query(res, x)$data$stmt_json) call inside vapply lacks error handling
and can break the vapply's expected character return; wrap the JSON parsing in a
tryCatch that first verifies query(res, x)$data$stmt_json exists and is
non-empty, attempt fromJSON inside tryCatch, check that the parsed object has a
character matches_hash field, and on any error or missing/invalid value return a
safe default (e.g., "" or NA_character_) so the vapply always returns a
character vector and failures are logged or flagged for debugging.

stringsAsFactors = FALSE
)
Expand Down
Loading