Learn R Programming

conditions (version 0.1)

assertion_message: Generate a custom condition

Description

condition creates a custom condition. The functions condition_message, condition_warning and condition_error are specialized to create conditions of type “message”, “warning” or “error”, respectively. Furthermore, the constructors for some standardized conditions are predefined (see details).

Usage

assertion_message(message, call = sys.call(-1L), attach = NULL)
deprecated_message(message, call = sys.call(-1L), attach = NULL)
dimension_message(message, call = sys.call(-1L), attach = NULL)
future_message(message, call = sys.call(-1L), attach = NULL)
index_message(message, call = sys.call(-1L), attach = NULL)
io_message(message, call = sys.call(-1L), attach = NULL)
length_message(message, call = sys.call(-1L), attach = NULL)
library_message(message, call = sys.call(-1L), attach = NULL)
lookup_message(message, call = sys.call(-1L), attach = NULL)
missing_message(message, call = sys.call(-1L), attach = NULL)
name_message(message, call = sys.call(-1L), attach = NULL)
runtime_message(message, call = sys.call(-1L), attach = NULL)
type_message(message, call = sys.call(-1L), attach = NULL)
value_message(message, call = sys.call(-1L), attach = NULL)
assertion_warning(message, call = sys.call(-1L), attach = NULL)
deprecated_warning(message, call = sys.call(-1L), attach = NULL)
dimension_warning(message, call = sys.call(-1L), attach = NULL)
future_warning(message, call = sys.call(-1L), attach = NULL)
index_warning(message, call = sys.call(-1L), attach = NULL)
io_warning(message, call = sys.call(-1L), attach = NULL)
length_warning(message, call = sys.call(-1L), attach = NULL)
library_warning(message, call = sys.call(-1L), attach = NULL)
lookup_warning(message, call = sys.call(-1L), attach = NULL)
missing_warning(message, call = sys.call(-1L), attach = NULL)
name_warning(message, call = sys.call(-1L), attach = NULL)
runtime_warning(message, call = sys.call(-1L), attach = NULL)
type_warning(message, call = sys.call(-1L), attach = NULL)
value_warning(message, call = sys.call(-1L), attach = NULL)
assertion_error(message, call = sys.call(-1L), attach = NULL)
deprecated_error(message, call = sys.call(-1L), attach = NULL)
dimension_error(message, call = sys.call(-1L), attach = NULL)
future_error(message, call = sys.call(-1L), attach = NULL)
index_error(message, call = sys.call(-1L), attach = NULL)
io_error(message, call = sys.call(-1L), attach = NULL)
length_error(message, call = sys.call(-1L), attach = NULL)
library_error(message, call = sys.call(-1L), attach = NULL)
lookup_error(message, call = sys.call(-1L), attach = NULL)
missing_error(message, call = sys.call(-1L), attach = NULL)
name_error(message, call = sys.call(-1L), attach = NULL)
runtime_error(message, call = sys.call(-1L), attach = NULL)
type_error(message, call = sys.call(-1L), attach = NULL)
value_error(message, call = sys.call(-1L), attach = NULL)
condition(type, class = character(0L), message, call = sys.call(-1L))
condition_error(class, message, call = sys.call(-1L), attach = NULL)
condition_warning(class, message, call = sys.call(-1L), attach = NULL)
condition_message(class, message, call = sys.call(-1L), attach = NULL)

Arguments

message
[character(1)] Information about the condition.
call
[call | NULL] Call stack.
attach
[ANY] Object to attach to the condition. Can be accessed via cond$attached in a tryCatch (see example).
type
[character(1)] Should be one of “error”, “warning” or “message”.
class
[character] Class for the condition. The functions condition_error, condition_warning and condition_message automatically append the respective type with an underscore (see example).

Value

[condition].

Details

The standardized conditions include:

Examples

Run this code
# A simple IO error:
e = condition_error("io", "Failed to load file")
print(e)
class(e)

# To signal the condition, use message/warning/stop.
## Not run: 
# message(e)
# warning(e)
# stop(e)
# ## End(Not run)

# These are equivalent (except the call):
w1 = condition("warning", "dimension_warning", "foo")
w2 = condition_warning("dimension", "foo")
w3 = tryCatch(dimension_warning("foo"), condition = function(e) e)

# Attach and retrieve additional information
f = function(x) {
  if(!is.numeric(x))
    assertion_error(" must be numeric", attach = x)
  x^2
}
f(1:10)

tryCatch(f(letters), assertion_error = function(e) {
  message("x must be numeric, but is ", typeof(e$attached))
})

Run the code above in your browser using DataLab