composerr: Compose error handlers (concatenate error messages)

Description Usage Arguments Value Examples

View source: R/composerr.R

Description

The functions composerr(), composerr_() and composerr_parent() modify error handlers by appending character strings to the error messages of the error handling functions:

Usage

1
2
3
4
5
6
7
8
composerr_(text_1 = NULL, err_prior = NULL, text_2 = NULL,
  sep_1 = ": ", sep_2 = ": ", env_prior = parent.frame())

composerr(text_1 = NULL, err_prior = NULL, text_2 = NULL,
  sep_1 = ": ", sep_2 = ": ", env_prior = parent.frame())

composerr_parent(text_1 = NULL, err_prior = NULL, text_2 = NULL,
  sep_1 = ": ", sep_2 = ": ", env_prior = parent.frame())

Arguments

text_1

A character string, which will be appended at the beginning of the error message. The argument sep_1 will be used as text separator.

err_prior

There are three valid types:

  • err_prior is omitted: A new error handling message will be returned.

  • composerr_ is the calling function: err_prio must be a character string holding the name of the error handling function to which the message part should be appended.

  • composerr is the calling function: err_prio must be the error handling function to which the message part should be appended.

text_2

A character string, which will be appended at the end of the error message. The argument sep_2 will be used as text separator.

sep_1

A character string that is used as separator for the concatenation of text_1 at the beginning of the error message.

sep_2

A character string that is used as separator for the concatenation of text_2 at the end of the error message.

env_prior

An environment where the error handling function given in err_prior can be found. If no environment is given, then the err_prior will be looked up in the current environment. In the situation of nested scopes, you may change the lookup environment to the parent environment in order to be able to recursively override the name of the error handling function. In order to keep it simple, the function composerr_parent() can be used instead.

Value

A new error handling function that has an extended error message.

Examples

 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
## Not run: 
# ------     composerr_ in flat situation      ----------
# -- create a modified error handler in the same scope --
# check if variable 'obj' exists and holds value TRUE
obj <- FALSE
# original error handler
err_h <- composerr_("Something is wrong with obj")
if (!exists("obj"))
  err_h("obj does not exist")
# create more precise error handler (same scope)
err_h2 <- composerr_("obj has wrong value", "err_h")
if (!obj)
  err_h2("Value is FALSE")
#--- resulting error ---
# "Something is wrong with obj: obj has wrong value: Value is FALSE"
## End(Not run)
## Not run: 
# ------      composerr in flat situation      ----------
# -- create a modified error handler in the same scope --
# check if variable 'obj' exists and holds value TRUE
obj <- FALSE
# original error handler
err_h <- composerr("Something is wrong with obj")
if (!exists("obj"))
  err_h("obj does not exist")
# create more precise error handler (same scope)
err_h2 <- composerr("obj has wrong value", err_h)
if (!obj)
  err_h2("Value is FALSE")
#--- resulting error ---
# "Something is wrong with obj: obj has wrong value: Value is FALSE"
## End(Not run)
## Not run: 
# ------  composerr_parent in nested situation   --------
# -- overwrite error handler in the deeper level scope --
# check if all entries of the list object 'obj' are TRUE
obj <- list(x = TRUE, y = TRUE, z = FALSE)
# original error handler
err_h <- composerr("obj is invalid")
# check each list element 
sapply(names(obj), function(name) {
  # modify error handler to nested sitation
  err_h <- composerr_parent(paste("Error in", name), err_h)
  # check element and throw error FALSE
  if (!obj[[name]])
    err_h("Value is FALSE")
})
#--- resulting error ---
# "obj is invalid: Error in z: Value is FALSE"
## End(Not run)

R-package/composerr documentation built on Oct. 30, 2019, 10:46 p.m.