File: conditions.R

package info (click to toggle)
r-cran-assertive.base 0.0-9-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 476 kB
  • sloc: sh: 13; makefile: 2
file content (80 lines) | stat: -rw-r--r-- 2,217 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#' Condition classes
#' 
#' Error, warning, and message classes derived from their simple equivalents.
#' @param message A string describing the problem.
#' @param call A call describing the source of the condition.
#' @param predicate_name A string naming the predicate that was called when the 
#' condition occured.
#' @return An object of class \code{assertionError}, \code{assertionWarning}, or
#' \code{assertionMessage}.
#' @note These objects behave the same as the standard-issue \code{simpleError},
#' \code{simpleWarning}, and \code{simpleMessage} objects from base-R.  The
#' extra class allows you to provide custom handling for assertions inside 
#' \code{tryCatch}.
#' @examples 
#' tryCatch(
#'   assert_all_are_true(FALSE), 
#'   error = function(e) 
#'   {
#'     if(inherits(e, "assertionCondition"))
#'     {
#'       # Handle assertions
#'       message("This is an assertion condition.")
#'       
#'       # Handle assertions cause by a specific predicate
#'       if(e$predicate_name == "is_true")
#'       {
#'       }
#'     } else
#'     {
#'       # Handle other error types
#'     }
#'   }
#' )
#' @export
assertionError <- function(message, call = NULL, predicate_name = NULL)
{
  aerr <- list(
    message = as.character(message), 
    call = call,
    predicate_name = predicate_name
  )
  class(aerr) <- c(
    "assertionError", "assertionCondition", 
    "simpleError", "error", "condition"
  )
  aerr
}

#' @rdname assertionError
#' @export
assertionWarning <- function(message, call = NULL, predicate_name = NULL)
{
  awrn <- list(
    message = as.character(message), 
    call = call,
    predicate_name = predicate_name
  )
  class(awrn) <- c(
    "assertionWarning", "assertionCondition", 
    "simpleWarning", "warning", "condition"
  )
  awrn
}

#' @rdname assertionError
#' @export
assertionMessage <- function(message, call = NULL, predicate_name = NULL)
{
  amsg <- list(
    message = as.character(message), 
    call = call,
    predicate_name = predicate_name
  )
  class(amsg) <- c(
    "assertionMessage", "assertionCondition", 
    "simpleMessage", "message", "condition"
  )
  amsg
}