From ee0a1b87f21e618cace3e5e9e41975582de80bb5 Mon Sep 17 00:00:00 2001 From: shikokuchuo <53399081+shikokuchuo@users.noreply.github.com> Date: Fri, 27 Oct 2023 14:55:59 +0100 Subject: [PATCH] improved OANDA error handling --- NEWS.md | 2 ++ R/switch.R | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/NEWS.md b/NEWS.md index 534224ae..5c6ecf93 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ichimoku 1.4.9.9000 (development) +* Improved handling for OANDA API errors. + # ichimoku 1.4.9 * Internal performance enhancements. diff --git a/R/switch.R b/R/switch.R index 00113a90..09dcabcb 100644 --- a/R/switch.R +++ b/R/switch.R @@ -121,6 +121,9 @@ do_ <- function() { resp <- ncurl(url, convert = FALSE, follow = TRUE, headers = c("Authorization" = paste0("Bearer ", apikey), "User-Agent" = .user_agent)) + resp[["status"]] == 200L || + stop("status code ", resp[["status"]], " - ", + deserialize_json(resp[["data"]]), call. = FALSE) parsed <- deserialize_json(resp[["data"]]) length(parsed[["accounts"]]) || stop(parsed, call. = FALSE) account <<- parsed[["accounts"]][[1L]][["id"]] @@ -137,11 +140,9 @@ do_ <- function() { resp <- ncurl(url, convert = FALSE, follow = TRUE, headers = c("Authorization" = paste0("Bearer ", apikey), "User-Agent" = .user_agent)) - if (resp[["status"]] != 200L) { - resp <- ncurl(url, convert = FALSE, follow = TRUE, - headers = c("Authorization" = paste0("Bearer ", apikey), - "User-Agent" = .user_agent)) - } + resp[["status"]] == 200L || + stop("status code ", resp[["status"]], " - ", + deserialize_json(resp[["data"]]), call. = FALSE) parsed <- deserialize_json(resp[["data"]]) length(parsed[["instruments"]]) || { warning(parsed,