Skip to content

Commit

Permalink
code update
Browse files Browse the repository at this point in the history
added trycatch for fetch and related helper functions
  • Loading branch information
sameerpadhye committed Sep 12, 2024
1 parent 1a04458 commit 3d33a8e
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 87 deletions.
23 changes: 1 addition & 22 deletions R/bold.fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,13 @@ bold.fetch<-function(param.data,

{

stop("Input params can only be either processid, sampleid, datasets or bin_uri")
stop("Input params can only be either processid, sampleid, dataset or project_codes and bin_uri")

return(FALSE)

}


if(nrow(json.df)==0)

{

stop("Data could not be retrieved. Please re-check the parameters.")

}


# a separate filter function is used to filter the retrieved data

Expand Down Expand Up @@ -396,19 +388,6 @@ bold.fetch<-function(param.data,

}

# If the resulting data (after filtering) is empty

if(nrow(json.df)==0)

{

stop("The output has no data.It could either be due to wrong range of values or incorrect names. Please re-check the filter/s used")

return(FALSE)

}



# If user wants to export the data

Expand Down
23 changes: 12 additions & 11 deletions R/helper.connect.pvt.fetch.bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ fetch.bold.bins<-function(data.input,
}


# # removing empty results
#
# result = Filter(function(df) nrow(df) > 0, result.pre.filter)


# Generating the data frame


Expand Down Expand Up @@ -175,12 +170,18 @@ fetch.bold.bins<-function(data.input,

# Convert the 'coord' character data into two numeric columns 'lat','lon'

json.df = json.df%>%
tidyr::separate(coord,
c("lat","lon"),
sep=",",
remove = T)%>%
dplyr::mutate(across(c(lat,lon), ~ as.numeric(.x)))
json.df <- tryCatch({
json.df %>%
tidyr::separate(coord, c("lat", "lon"),
sep = ",",
remove = TRUE) %>%
dplyr::mutate(across(c(lat, lon),
~ as.numeric(.x)))
}, error = function(e) {
message("Error occurred during data download. Please re-check the param.data, query.param, param.index or the api_key.")
return(NULL)
})


return(json.df)

Expand Down
14 changes: 8 additions & 6 deletions R/helper.connect.pvt.fetch.datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,14 @@ fetch.bold.datasets<-function(data.input,

# Convert the 'coord' character data into two numeric columns 'lat','lon'

json.df = json.df%>%
tidyr::separate(coord,
c("lat","lon"),
sep=",",
remove = T)%>%
dplyr::mutate(across(c(lat,lon), ~ as.numeric(.x)))
json.df <- tryCatch({
json.df %>%
tidyr::separate(coord, c("lat", "lon"), sep = ",", remove = TRUE) %>%
dplyr::mutate(across(c(lat, lon), ~ as.numeric(.x)))
}, error = function(e) {
message("Error occurred during data download. Please re-check the param.data, query.param, param.index or the api_key.")
return(NULL)
})

return(json.df)

Expand Down
34 changes: 10 additions & 24 deletions R/helper.connect.pvt.fetch.processids.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fetch.bold.processid<-function(data.input,

{

stop("Error occurred during data download. Please re-check param.data,param.query, param.index or the api_key")
stop("Error occurred during data download. Please re-check the param.data, query.param, param.index or the api_key.")

}

Expand All @@ -127,34 +127,20 @@ fetch.bold.processid<-function(data.input,
dplyr::bind_rows(.)


if(nrow(json.df)==0)

{

stop("Search resulted in an empty dataset. Please re-check the input data.")

}

else

{

json.df

}

}
}



# Convert the 'coord' character data into two numeric columns 'lat','lon'

json.df = json.df%>%
tidyr::separate(coord,
c("lat","lon"),
sep=",",
remove = T)%>%
dplyr::mutate(across(c(lat,lon), ~ as.numeric(.x)))
json.df <- tryCatch({
json.df %>%
tidyr::separate(coord, c("lat", "lon"), sep = ",", remove = TRUE) %>%
dplyr::mutate(across(c(lat, lon), ~ as.numeric(.x)))
}, error = function(e) {
message("Error occurred during data download. Please re-check the param.data, query.param, param.index or the api_key.")
return(NULL)
})



Expand Down
15 changes: 9 additions & 6 deletions R/helper.connect.pvt.fetch.projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,15 @@ fetch.bold.project<-function(data.input,

# Convert the 'coord' character data into two numeric columns 'lat','lon'

json.df = json.df%>%
tidyr::separate(coord,
c("lat","lon"),
sep=",",
remove = T)%>%
dplyr::mutate(across(c(lat,lon), ~ as.numeric(.x)))
json.df <- tryCatch({
json.df %>%
tidyr::separate(coord, c("lat", "lon"), sep = ",", remove = TRUE) %>%
dplyr::mutate(across(c(lat, lon), ~ as.numeric(.x)))
}, error = function(e) {
message("Error occurred during data download. Please re-check the param.data, query.param, param.index or the api_key.")
return(NULL)
})


return(json.df)

Expand Down
15 changes: 9 additions & 6 deletions R/helper.connect.pvt.fetch.sampleids.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ fetch.bold.sampleid<-function(data.input,

# Convert the 'coord' character data into two numeric columns 'lat','lon'

json.df = json.df%>%
tidyr::separate(coord,
c("lat","lon"),
sep=",",
remove = T)%>%
dplyr::mutate(across(c(lat,lon), ~ as.numeric(.x)))
json.df <- tryCatch({
json.df %>%
tidyr::separate(coord, c("lat", "lon"), sep = ",", remove = TRUE) %>%
dplyr::mutate(across(c(lat, lon), ~ as.numeric(.x)))
}, error = function(e) {
message("Error occurred during data download. Please re-check the param.data, query.param, param.index or the api_key.")
return(NULL)
})



return(json.df)
Expand Down
22 changes: 16 additions & 6 deletions R/helper.get_data4connect.pvt.fetch.bins.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ get_data_bins<-function (data_for_bins,
{

# Handle the error for the POST request
message("An error occurred during data download: ", e$message)

return(NULL)
stop("An error occurred during data download: ", e$message)


}
Expand Down Expand Up @@ -80,10 +78,22 @@ get_data_bins<-function (data_for_bins,
"\n")[[1]], # split the content (here each process or sample id)
function(x) fromJSON(x)) # convert to JSON string and lapply converts that into a list

# COnvert the list to a data frame
# Convert the list to a data frame

input_data2 <- tryCatch({
df <- json_data_bins %>%
data.frame(.)

if (nrow(df) == 0) {
stop("The resulting data frame is empty.")
}

df
}, error = function(e) {
# Handle the error for the data frame conversion
stop("Error occurred during data download. Please re-check the param.data, query.param, param.index or the api_key.")

input_data2 = json_data_bins%>%
data.frame(.)
})


return(input_data2)
Expand Down
17 changes: 14 additions & 3 deletions R/helper.get_data4connect.pvt.fetch.dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,22 @@ get_data_datasets<-function (data_for_datasets,
"\n")[[1]], # split the content (here each process or sample id)
function(x) fromJSON(x)) # convert to JSON string and lapply converts that into a list

# COnvert the list to a data frame
# Convert the list to a data frame

input_data2 = json_data_datasets%>%
data.frame(.)
input_data2 <- tryCatch({
df <- json_data_datasets %>%
data.frame(.)

if (nrow(df) == 0) {
stop("The resulting data frame is empty.")
}

df
}, error = function(e) {
# Handle the error for the data frame conversion
stop("Error occurred during data download. Please re-check the param.data, query.param, param.index or the api_key.")

})

return(input_data2)

Expand Down
18 changes: 15 additions & 3 deletions R/helper.get_data4connect.pvt.fetch.project.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,22 @@ get_data_projects<-function (data_for_projects,
"\n")[[1]], # split the content (here each process or sample id)
function(x) fromJSON(x)) # convert to JSON string and lapply converts that into a list

# COnvert the list to a data frame
# Convert the list to a data frame

input_data2 = json_data_project%>%
data.frame(.)
input_data2 <- tryCatch({
df <- json_data_project %>%
data.frame(.)

if (nrow(df) == 0) {
stop("The resulting data frame is empty.")
}

df
}, error = function(e) {
# Handle the error for the data frame conversion
stop("Error occurred during data download. Please re-check the param.data, query.param, param.index or the api_key.")

})


return(input_data2)
Expand Down

0 comments on commit 3d33a8e

Please sign in to comment.