diff options
Diffstat (limited to 'R/http.R')
| -rw-r--r-- | R/http.R | 14 |
1 files changed, 12 insertions, 2 deletions
@@ -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 { |