summaryrefslogtreecommitdiffstats
path: root/R/http.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/http.R')
-rw-r--r--R/http.R14
1 files changed, 12 insertions, 2 deletions
diff --git a/R/http.R b/R/http.R
index 6ef4511..08e8ddc 100644
--- a/R/http.R
+++ b/R/http.R
@@ -173,12 +173,22 @@ json_object <- function(body) {
data
}
-api_message <- function(body, fallback) {
+# The counterpart of json_object() for a body that is already an error: it
+# never signals, because a broken error body must not replace the error the
+# caller is being told about. Missing, malformed, empty: all of them NULL.
+json_field <- function(body, name) {
data <- tryCatch(
jsonlite::parse_json(decode_body(body), simplifyVector = FALSE),
error = function(e) NULL
)
- message <- if (is.list(data)) data[["mensagem"]] else NULL
+ if (!is.list(data) || is.null(names(data))) {
+ return(NULL)
+ }
+ data[[name]]
+}
+
+api_message <- function(body, fallback) {
+ message <- json_field(body, "mensagem")
if (is.character(message) && length(message) == 1L && nzchar(message)) {
message
} else {