@@ -251,39 +251,52 @@ upload_current_exercise <- function(credentials,
251
251
project_path ,
252
252
zip_name = " temp" ,
253
253
remove_zip = TRUE ) {
254
- response <- list ()
255
- .dprint(" upload_current_exercise()" )
256
- metadata <- tryCatch({
254
+ read_metadata <- function () {
257
255
json <- base :: list.files(path = project_path ,
258
256
pattern = " .metadata.json" ,
259
257
all.files = TRUE ,
260
258
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 ,
272
282
project_path = project_path ,
273
283
server_address = address ,
274
284
zip_name = zip_name ,
275
285
remove_zip = remove_zip )
276
- response
286
+ return ( response )
277
287
}, error = function (e ) {
278
288
cat(" Uploading exercise failed.\n " )
279
- response $ error <- e
280
- response
289
+ stop(e $ message )
281
290
})
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
+
287
300
}
288
301
289
302
# ' @title Get all TMC organizations
0 commit comments