tests/testthat/test-connection.R

test_that("SAD: catches API connectivity error", {
  dead_api <- \(endpoint, key, json_body) {
    result::failure("Connection Error to OpenAI API", status = "Error")
  }

  with_stubbed_credentials({
    bad <- \() {
      air::howto(
        "How do I get the first element of a list?",
        call_api = dead_api
      )
    }

    # Should throw a helpful error
    expect_error(bad(), "Connection Error")
  })
})

Try the air package in your browser

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

air documentation built on Aug. 8, 2025, 6:46 p.m.