Skip to content

Error handing with status 200 #98

@selesnow

Description

@selesnow

Hello,

I try to use req_error() for handing next respons:

<httr2_response>
GET https://api.tgstat.ru/usage/stat?token=asdfghjklkjhgfd
Status: 200 OK
Content-Type: application/json
Body: In memory (42 bytes)
resp_body_json(resp)
$status
[1] "error"

$error
[1] "token_invalid"

And i try use

tg_error_body <- function(resp) {
  resp %>% resp_body_json() %>% .$error
}

tg_is_error <- function(resp) {
  resp_stat <- resp %>% resp_body_json() %>% .$status
  resp_status(resp) == 200 && resp_stat == "error"
}

resp <- request(str_glue('{getOption("tg.base_url")}')) %>%
    req_url_path_append(method) %>%
    req_url_query( token  = 'False access token') %>%
    req_user_agent("rtgstat") %>%
    req_retry(
      max_tries    = getOption('tg.max_tries'),
      is_transient = ~ !is_response(.x) && resp_status(.x) != 200,
      after        = getOption('tg.interval')
    ) %>%
    req_error(is_error = function(resp) TRUE, body = tg_error_body) %>%
    req_perform() %>%
    resp_body_json()

And it does not work, could you help, how i can handing error response with status 200?

minimal reprex

request("http://httpbin.org/") %>%
  req_error(is_error = function(resp) TRUE) %>%
  req_perform()

<httr2_response>
GET http://httpbin.org/
Status: 200 OK
Content-Type: text/html
Body: In memory (9593 bytes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions