Skip to content

Commit 36296d9

Browse files
Improving submission error handling 1
1 parent a1103fb commit 36296d9

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

tmcrstudioaddin/R/HTTPQueries.R

+35-22
Original file line numberDiff line numberDiff line change
@@ -251,39 +251,52 @@ upload_current_exercise <- function(credentials,
251251
project_path,
252252
zip_name = "temp",
253253
remove_zip = TRUE) {
254-
response <- list()
255-
.dprint("upload_current_exercise()")
256-
metadata <- tryCatch({
254+
read_metadata <- function() {
257255
json <- base::list.files(path = project_path,
258256
pattern = ".metadata.json",
259257
all.files = TRUE,
260258
full.names = TRUE)
261-
jsonlite::fromJSON(txt = json, simplifyVector = FALSE)
262-
}, error = function(e) {
263-
NULL
264-
})
265-
response <-
266-
if (!is.null(metadata$id[[1]])) {
267-
id <- metadata$id[[1]]
268-
token <- credentials$token
269-
address <- paste(sep = "", credentials$serverAddress, "/")
270-
tryCatch({
271-
response <- upload_exercise(token = token, exercise_id = id,
259+
if (length(json) == 0) {
260+
stop("Corrupted project: missing RTMC metadata")
261+
}
262+
if (length(json) > 1) {
263+
stop("Corrupted project: multiple RTMC metadata")
264+
}
265+
metadata <- jsonlite::fromJSON(txt = json, simplifyVector = FALSE)
266+
return(metadata)
267+
}
268+
metadata_to_id <- function(metadata) {
269+
if (is.null(metadata$id) || is.na(metadata$id)) {
270+
stop("RTMC metadata read, but metadata is corrupted")
271+
}
272+
return(metadata$id[[1]])
273+
}
274+
upload_with_id <- function(id) {
275+
token <- credentials$token
276+
address <- paste(sep = "", credentials$serverAddress, "/")
277+
#
278+
# uploading starts
279+
tryCatch({
280+
response <- upload_exercise(token = credentials$token,
281+
exercise_id = id,
272282
project_path = project_path,
273283
server_address = address,
274284
zip_name = zip_name,
275285
remove_zip = remove_zip)
276-
response
286+
return(response)
277287
}, error = function(e) {
278288
cat("Uploading exercise failed.\n")
279-
response$error <- e
280-
response
289+
stop(e$message)
281290
})
282-
} else {
283-
response$error <- "Could not read json"
284-
response
285-
}
286-
return(response)
291+
}
292+
tryCatch(upload_with_id(metadata_to_id(read_metadata())),
293+
error = function(e) {
294+
response <- list(data = list(),
295+
error = e)
296+
response$error$server_access <- FALSE
297+
return(response)
298+
})
299+
287300
}
288301

289302
#' @title Get all TMC organizations

tmcrstudioaddin/R/Submissions.R

+6-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ submit_current <- function(path, credentials) {
7676
} else {
7777
.dprint("HERE NOW. NO CONNECTION TO SERVER")
7878
cat("No access to server.\n")
79-
submitJson$error <- response$error
80-
submitJson$error$server_access <- FALSE
79+
submitJson2 <- list()
80+
submitJson2$error <- response$error
81+
submitJson2$error$server_access <- FALSE
82+
submitJson <- list(error = list(message = response$error$message,
83+
call = response$error$call,
84+
server_access = FALSE))
8185
}
8286
return(submitJson)
8387
}

0 commit comments

Comments
 (0)