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

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") })
#> <gradethis_graded: [Correct] message 1>
# Fails with message from fail() eval_gradethis({ fail("incorrect") pass("correct") })
#> <gradethis_graded: [Incorrect] incorrect>
# Fails with message from expect_true() eval_gradethis({ testthat::expect_true(FALSE) pass("message 2") pass("message 3") })
#> <gradethis_graded: [Incorrect] #> FALSE is not TRUE #> #> `actual`: FALSE #> `expected`: TRUE #> >
# Fails immediately with message "boom" eval_gradethis({ stop("boom") pass("message 2") pass("message 3") })
#> <gradethis_graded: [Incorrect] boom>