After you hang up your typical R error messages, they are quite useful.
However, I often find my own R error messages are not as informative as I expected, so I usually write some kind of personalized error messages.
Now I wonder if there is any kind of systematic approach (or what the recommended best practice approach will be) regarding the assignment of unique error codes, as well as additional information for specific errors.
I suppose I would like to create some kind of error hash table solution where additional information can be stored and retrieved for each error. Would you use some kind of βlightweightβ database solution (for example, SQLite) or would you solve it with a simple data.frame or list object that is stored somewhere ( .rdata ) and retrieved if necessary?
Homework
Honestly, I have not done this much research yet. Although there is at least something; -)
I just found out about the reportr package and will check it in more detail. Does anyone have experience with this package or similar?
Scheme of my own approach (partially pseudocode)
myFoo <- function(x, ...) { tryCatch( x * 100, error=function(e) { record <- retrieveErrorRecord(e) # Does not exist yet # 'record' would be some sort of list or Ref Class Object if (!length(record)) { uid <- generateUid(e) # Does not exist yet msg <- paste( "expecting arg 'x' to be of class 'numeric' (was '", class(x), "')", sep="") insertErrorRecord( # Does not exist yet list( uid=uid, message=msg, original=e ) ) record <- retrieveErrorRecord(e) } msg <- c( "myFoo/error:\n", paste("* Code: ", record$uid, "\n", sep=""), paste("* Message: ", record$message, "\n", sep=""), paste("* Original: ", record$original, "\n", sep="") ) stop(msg) } ) }
The way the message will look
require("digest") x <- "abc" e <- simpleError("test error") record <- list( uid=digest(e), message=paste("expecting arg 'x' to be of class 'numeric' (was '", class(x), "')", sep=""), original=e ) msg <- c( "myFoo/error:\n", paste("* Code: ", record$uid, "\n", sep=""), paste("* Message: ", record$message, "\n", sep=""), paste("* Original: ", record$original, "\n", sep="") ) > stop(msg) Error: myFoo/error: * Code: e78e73054b93d2bf682df32845cd064d * Message: expecting arg 'x' to be of class 'numeric' (was 'character') * Original: Error: test error