Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

cnd_message

Build an error message from parts


Description

cnd_message() assembles an error message from three generics:

  • cnd_header()

  • cnd_body()

  • cnd_footer()

The default method for the error header returns the message field of the condition object. The default methods for the body and footer return empty character vectors. In general, methods for these generics should return a character vector. The elements are combined into a single string with a newline separator.

cnd_message() is automatically called by the conditionMessage() for rlang errors. Error classes created with abort() only need to implement header, body or footer methods. This provides a lot of flexibility for hierarchies of error classes, for instance you could inherit the body of an error message from a parent class while overriding the header and footer.

Usage

cnd_message(cnd)

cnd_header(cnd, ...)

cnd_body(cnd, ...)

cnd_footer(cnd, ...)

Arguments

cnd

A condition object.

...

Arguments passed to methods.

Overriding cnd_body()

Experimental lifecycle

Sometimes the contents of an error message depends on the state of your checking routine. In that case, it can be tricky to lazily generate error messages with cnd_body(): you have the choice between overspecifying your error class hierarchies with one class per state, or replicating the type-checking control flow within the cnd_body() method. None of these options are ideal.

A better option is to define a body field in your error object containing a static string, a lambda-formula, or a function with the same signature as cnd_body(). This field overrides the cnd_body() generic and makes it easy to generate an error message tailored to the state in which the error was constructed.


rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.