Is there a way to directly extend/augment/overwrite the original error messages that are thrown when calls to functions that I didn't write myself fail (i.e. functions from base R and contributed packages)?
Example
Consider the following Reference Class:
setRefClass("A", fields=list(x1="character"))
No error when specifying correct values:
new("A", x1="Hello World!")
Resulting error when specifying wrong values:
> new("A", x1=TRUE)
Error: invalid assignment for reference class field 'x1', should be from class "character" or a subclass (was class "logical")
Now, I'd like to include information about the class that actually "caused the problem".
Maybe this could look something like this:
Error: Field assignment error in class 'A':
Invalid assignment for reference class field 'x1', should be from class "character" or a subclass (was class "logical")
My current workarounds for realizing something close often look like this:
setRefClass("A",
fields=list(x1="character"),
methods=list(
setField=function(field, value) {
tryCatch(
.self$field(name=field, value=value),
warning=function(cond) {
message(cond)
.self$field(field=field, value=value)
},
error=function(cond) {
stop(paste0("Field assignment error in class '",
class(.self), "'\n"),
"ORIGINAL ERROR:\n", as.character(cond)
)
}
)
}
)
)
After instantiating a "blank" object first
x <- new("A")
this would give me the following error when trying to set wrong values to the field via the explicit setter method:
> x$setField(field="x1", value=TRUE)
Error in value[[3L]](cond) : Field assignment error in class 'A'
ORIGINAL ERROR:
Error: invalid assignment for reference class field 'x1', should be from class "character" or a subclass (was class "logical")
I don't like it for the following reasons:
It's a lot of code for just a little extra information and the
tryCatch()
part makes the code somewhat harder to read.Due to the nature of
tryCatch()
, AFAIU I'm obliged to also put the.self$field(field=field, value=value)
statement within thewarning()
function if I expect somewhat "regular" behavior (i.e. "even though there was a warning, the field value is still set"). That's duplicated code.Seems to me that coercing error messages via
as.character()
(AFAIU necessary in order to being able to add information viapaste()
or the like) sometimes cuts the error message off.That way I'm forced to set my field values via explicit setter functions (
<obj>$setField(<field>, <value>)
) as opposed to setting them via the "built-in"initialize
function (new("A", <field>=<value>)
)