eval_gradethis: Capture grades and errors

View source: R/graded_condition.R

eval_gradethisR Documentation

Capture grades and errors

Description

Capture the first graded() signal or error thrown when evaluating the expr.

Usage

eval_gradethis(expr, on_error = NULL, on_graded = NULL)

Arguments

expr

The expression or code block to evaluate

on_error

A withCallingHandlers() handler for class error with signature ⁠function(error, this_env)⁠ that receives the error object and calling environment of the error handler. on_error should use rlang::return_from() using this_env to immediately return the value and not continue evaluation.

on_graded

A withCallingHandlers() handler for class graded with signature ⁠function(grade, this_env)⁠ that receives the error object and calling environment of the error handler. on_graded should use rlang::return_from() using this_env to immediately return the value and not continue evaluation.

Examples

# Passes with "message 1", short-circuiting evaluation
eval_gradethis({
  pass("message 1")
  pass("message 2")
  pass("message 3")
})

# Fails with message from fail()
eval_gradethis({
  fail("incorrect")
  pass("correct")
})

# Fails with message from expect_true()
eval_gradethis({
  testthat::expect_true(FALSE)
  pass("message 2")
  pass("message 3")
})

# Fails immediately with message "boom"
eval_gradethis({
  stop("boom")
  pass("message 2")
  pass("message 3")
})

rstudio-education/grader documentation built on July 6, 2023, 8:48 a.m.