Skip to content

Commit

Permalink
added 'success and error functions' to a doc page and exported
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfischetti committed Feb 5, 2017
1 parent 4394241 commit f28212e
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ Suggests:
testthat,
magrittr
VignetteBuilder: knitr
RoxygenNote: 5.0.1
RoxygenNote: 6.0.0
9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ export(assert)
export(assert_)
export(assert_rows)
export(assert_rows_)
export(error_append)
export(error_logical)
export(error_report)
export(error_return)
export(error_stop)
export(in_set)
export(insist)
export(insist_)
export(insist_rows)
export(insist_rows_)
export(just_warn)
export(maha_dist)
export(not_na)
export(num_row_NAs)
export(success_continue)
export(success_logical)
export(verify)
export(warn_report)
export(within_bounds)
export(within_n_mads)
export(within_n_sds)
59 changes: 59 additions & 0 deletions R/errors.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,66 @@ summary.assertr_verify_error <- function(error){ print(error) }

## DO THESE NEED TO BE EXPORTED!?!!?!


#' Success and error functions
#'
#' The behavior of functions like \code{assert}, \code{assert_rows},
#' \code{insist}, \code{insist_rows}, \code{verify} when the assertion
#' passes or fails is configurable via the \code{success_fun}
#' and \code{error_fun} parameters, respectively.
#' The \code{success_fun} parameter takes a function that takes
#' the data passed to the assertion function as a parameter. You can
#' write your own success handler function, but there are two
#' provided by this package:
#' \itemize{
#' \item \code{success_continue} - just returns the data that was
#' passed into the assertion function
#' \item \code{success_logical} - returns TRUE
#' }
#' The \code{error_fun} parameter takes a function that takes
#' the data passed to the assertion function as a parameter. You can
#' write your own error handler function, but there are a few
#' provided by this package:
#' \itemize{
#' \item \code{error_stop} - Prints a summary of the errors and
#' halts execution.
#' \item \code{error_report} - Prints all the information available
#' about the errors and halts execution.
#' \item \code{error_append} - Attaches the errors to a special
#' attribute of \code{data} and returns the data. This is chiefly
#' to allow assertr errors to be accumulated in a pipeline so that
#' all assertions can have a chance to be checked and so that all
#' the errors can be displayed at the end of the chain.
#' \item \code{error_logical} - returns FALSE
#' \item \code{just_warn} - Prints a summary of the errors but does
#' not halt execution, it just issues a warning.
#' \item \code{warn_report} - Prints all the information available
#' about the errors but does not halt execution, it just issues a warning.
#' }
#' @name success_and_error_functions
NULL



#########################
# success functions #
#########################

#' @export
#' @rdname success_and_error_functions
success_logical <- function(data, ...){ return(TRUE) }

#' @export
#' @rdname success_and_error_functions
success_continue <- function(data, ...){ return(data) }


#######################
# error functions #
#######################

#' @export
#' @rdname success_and_error_functions
error_stop <- function(errors, data=NULL, warn=FALSE, ...){
if(!is.null(data) && !is.null(attr(data, "assertr_errors")))
errors <- append(attr(data, "assertr_errors"), errors)
Expand All @@ -162,10 +209,14 @@ error_stop <- function(errors, data=NULL, warn=FALSE, ...){
# for backwards compatibility
assertr_stop <- error_stop

#' @export
#' @rdname success_and_error_functions
just_warn <- function(errors, data=NULL){
error_stop(errors, data, warn=TRUE)
}

#' @export
#' @rdname success_and_error_functions
error_report <- function(errors, data=NULL, warn=FALSE, ...){
if(!is.null(data) && !is.null(attr(data, "assertr_errors")))
errors <- append(attr(data, "assertr_errors"), errors)
Expand All @@ -181,23 +232,31 @@ error_report <- function(errors, data=NULL, warn=FALSE, ...){
return(data)
}

#' @export
#' @rdname success_and_error_functions
warn_report <- function(errors, data=NULL){
error_report(errors, data, warn=TRUE)
}

#' @export
#' @rdname success_and_error_functions
error_append <- function(errors, data=NULL){
if(is.null(attr(data, "assertr_errors")))
attr(data, "assertr_errors") <- list()
attr(data, "assertr_errors") <- append(attr(data, "assertr_errors"), errors)
return(data)
}

#' @export
#' @rdname success_and_error_functions
error_return <- function(errors, data=NULL){
if(!is.null(data) && !is.null(attr(data, "assertr_errors")))
errors <- append(attr(data, "assertr_errors"), errors)
return(errors)
}

#' @export
#' @rdname success_and_error_functions
error_logical <- function(errors, data=NULL, ...){
return(FALSE)
}
Expand Down
1 change: 0 additions & 1 deletion man/assert.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/assert_rows.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/assertr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/in_set.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/insist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/insist_rows.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/maha_dist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/not_na.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/num_row_NAs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/print.assertr_assert_error.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/print.assertr_verify_error.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions man/success_and_error_functions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/summary.assertr_assert_error.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/summary.assertr_verify_error.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/verify.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/within_bounds.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/within_n_mads.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/within_n_sds.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f28212e

Please sign in to comment.