tests/testthat/test_aws_error.R

test_that("error conditions have correct classes", {
  error_list <- list(
    code = "foo",
    message = "bar",
    status_code = 404,
    error_response = list(a = "baz", b = "pizza", c = list(1, 2))
  )
  error_condition <- aws_error(error_list)

  expect_equal(error_condition$message, "foo (HTTP 404). bar")
  expect_equal(attributes(error_condition)$status_code, 404)
  expect_equal(attributes(error_condition)$error_response$c[[1]], 1)
  expect_identical(attributes(error_condition)$error_response, list(a = "baz", b = "pizza", c = list(1, 2)))

  expect_error(stop(error_condition))
  expect_error(stop(error_condition), class = "http_error")
  expect_error(stop(error_condition), class = "http_400")
  expect_error(stop(error_condition), class = "http_404")
})

test_that("error with NA status codes don't have incorrect classes", {
  error_list <- list(
    code = "foo",
    message = "bar",
    status_code = NA_integer_,
    error_response = list(a = "baz", b = "pizza", c = list(1, 2))
  )
  error_condition <- aws_error(error_list)

  expect_equal(error_condition$message, "foo (HTTP NA). bar")
  expect_identical(attributes(error_condition)$status_code, NA_integer_)
  expect_equal(attributes(error_condition)$error_response$c[[1]], 1)
  expect_identical(attributes(error_condition)$error_response, list(a = "baz", b = "pizza", c = list(1, 2)))

  expect_error(stop(error_condition))
  expect_error(stop(error_condition), class = "http_error")
  expect_false(inherits(error_condition, "http_NA"))
  expect_false(inherits(error_condition, "http_NA_integer"))
  expect_false(inherits(error_condition, "http_na"))
  expect_false(inherits(error_condition, "http_na_integer"))
})

Try the paws.common package in your browser

Any scripts or data that you put into this service are public.

paws.common documentation built on Nov. 12, 2023, 1:08 a.m.