tests/testthat/test-errors.R

capture_error <- function(expression) {
  tryCatch(
    expression,
    error = function(error) error
  )
}

test_that("Biogeme errors carry a specific class and actionable details", {
  error <- capture_error(
    rbiogeme:::biogeme_abort(
      "The model is invalid.",
      class = "biogeme_specification_error",
      operation = "model compilation",
      suggestion = "check the utility alternatives"
    )
  )
  expect_s3_class(error, "biogeme_specification_error")
  expect_s3_class(error, "biogeme_error")
  expect_match(conditionMessage(error), "The model is invalid")
  expect_match(conditionMessage(error), "check the utility alternatives")
  details <- biogeme_error_details(error)
  expect_equal(details$class, "biogeme_specification_error")
  expect_equal(details$operation, "model compilation")
  expect_null(details$python_traceback)
})

test_that("invalid model and result inputs use typed conditions", {
  specification_error <- capture_error(rbiogeme:::biogeme_compile_model(list()))
  expect_s3_class(specification_error, "biogeme_specification_error")

  estimate_error <- capture_error(estimate(list()))
  expect_s3_class(estimate_error, "biogeme_specification_error")

  result_error <- capture_error(read_results("this-file-does-not-exist.yaml"))
  expect_s3_class(result_error, "biogeme_result_error")
})

test_that("Python failures can be classified without losing their cause", {
  parent <- simpleError("Python dependency is unavailable")
  error <- capture_error(
    rbiogeme:::biogeme_rethrow(
      parent,
      class = "biogeme_environment_error",
      operation = "Python environment initialization",
      suggestion = "configure a supported Python executable"
    )
  )
  expect_s3_class(error, "biogeme_environment_error")
  expect_identical(error$parent, parent)
  expect_match(conditionMessage(error), "Python dependency is unavailable")
  expect_match(conditionMessage(error), "configure a supported Python executable")
})

Try the rbiogeme package in your browser

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

rbiogeme documentation built on Sept. 29, 2026, 5:09 p.m.