MyNixOS website logo
Description

Classed Error and Warning Conditions.

This provides utilities for creating classed error and warning conditions based on where the error originated.

pkgcond

Travis build status Coverage status

The goal of pkgcond is to facilitate the creation of errors, warnings, and messages (collectively called signals or conditions) that are more informative than the base versions.

Signals can be created through pkg_error(), pkg_warning() and pkg_message(). When these are used a scope is computed and used to create errors, warnings and signals, respectively, with classes set to the combinations of the scope. The scope, while is could be set explicitly, infers where the condition is created, and will typically include the function call and package name.

Installation

You can install the released version of pkgcond from CRAN with:

install.packages("pkgcond")

Understanding Scope

Let's consider a toy example to understand the scope. Say that we are creating a package named hw with a function hello_world() defined as follows:

hello_world <- function(greeting = 'hello', who = 'world'){
    if (!is.character(greeting) && length(greeting) == 1L) 
        pkg_error("greeting must be a string.")
    if (!is.character(who) && length(who) == 1L) 
        pkg_error("who must be a string.")
    pkg_message(paste(greeting, who))
}

If the function is called with the defaults the pkg_message() function will be called. The effect is the same as if a base::message() call were here; A message with "hello world" would appear in the console. However, pkg_message() does a little more. The base call would create a 'message' object and signal it. With pkg_message() since it was invoked inside the hello_world() function inside the hw package the scope would be set to c('hw', 'hello_world') This in turn would be used to create the message signal with the following classes.

  • hw::hello_world-message
  • hw::hello_world-condition
  • hw-message
  • hw-condition
  • message
  • condition

The pkg_error() and pkg_warning() functions have similar scoping however with error and warning replacing message, respectively.

This becomes really useful if one wishes to capture conditions. This is done with the tryCatch() function. In this way one can easily catch the conditions that originate from a specific function call or a specific package while passing others through or handling them differently.

tryCatch( hello_world("die", stop)
        , "hw::hello_world-error" = function(cond){
            # This would handle the error that is raised from passing
            # `stop` into the `hello_world function()
            }
        , "hw-condition" = function(cond){
            # This would capture all error originating in the hw package
            # that were signaled using any of the `pkg_*` functions.
        }
        # Errors that did not originate from the hw package or were created 
        # with the traditional stop, warning, or message functions
        # will be passed through since they will not be "Caught"
)

Helpers included:

These functions are included with pkgcond to help create error messages.

  • assert_that() This intentionally masks assertthat::assert_that() when an assertion fails from this call the scope is set as above but a type is also created that indicates this is a 'assertion failure' that can also be used to catch specific errors.
  • comma_list() takes a list of items and creates a correctly formatted (for English at least) comma separated list contained in a single string.
  • collapse() takes a character vector and collapses it to a single string separated by a space.
  • collapse0() same as previous but no space separating parts.
  • lhs %<<% rhs an infix operator version of paste but will attempt to coerce and collapse lhs and rhs as well.
  • lhs %<<<% an infix operator version of paste0, will use collapse0 on lhs and rhs prior to concatenation of the two.
  • lhs %\% rhs Similar to the previous two but separates with a new line. This will use collapse prior to concatenation.
  • ._() Used to enable translation for a signal message. When used with a single argument acts as an alias for gettext() when given multiple arguments it wraps gettextf().

Documentation

The pkgcond package is developed by the R Documentation Task Force, an R ConsortiumInfrastructure Steering Committee working group.

Metadata

Version

0.1.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows