IƱaki Ucar
2023-Apr-16 11:52 UTC
[Rd] Unique ID for conditions to supress/rethrow selected conditions?
On Sun, 16 Apr 2023 at 12:58, nospam at altfeld-im.de <nospam at altfeld-im.de> wrote:> > I am the author of the *tryCatchLog* package and want to > > - suppress selected conditions (warnings and messages) > - rethrow selected conditions (e.g a specific warning as a message or to "rename" the condition text). > > I could not find any reliable unique identifier for each possible condition > > - that (base) R throws > - that 3rd-party packages can throw (out of scope here). > > Is there any reliable way to identify each possible condition of base R?I don't think so. As stated in the manual, "?simpleError? is the class used by ?stop? and all internal error signals".> Are there plans to implement such an identifier ("errno")?I agree that something like this would be a nice addition. With the current condition system, it would be certainly easy (but quite a lot of work) to define a hierarchy of built-in conditions, and then use them consistently throughout base R. For example,> 1 + "a"Error in 1 + "a" : non-numeric argument to binary operator could be a "typeError". And catching this is already possible:> e <- errorCondition("non-numeric argument to binary operator", class="typeError") > tryCatch(stop(e), typeError=function(e) print("hello"))[1] "hello" -- I?aki ?car
@osp@m m@iii@g oii @itieid-im@de
2023-Apr-16 14:49 UTC
[Rd] Unique ID for conditions to supress/rethrow selected conditions?
On Sun, 2023-04-16 at 13:52 +0200, I?aki Ucar wrote:> I agree that something like this would be a nice addition. With the > current condition system, it would be certainly easy (but quite a lot > of work) to define a hierarchy of built-in conditions, and then use > them consistently throughout base R.Yes, a typed condition system would be great. I have two other ideas: By reading the "R messages" and "preparing translactions" sections of the "R extensions manual" https://cran.r-project.org/doc/manuals/r-release/R-exts.html#R-messages I was thinking about using the "unique" R message texts (which are the msgid in the *.po files, see e.g. https://github.com/r-devel/r-svn/blob/60a4db2171835067999e96fd2751b6b42c6a6ebc/src/library/base/po/de.po#L892) to maintain a unique ID (not dependent on the actual translation into the current language). A "simple" solution could be to pre- or postfix each message text with an ID, for example this code here else errorcall(call, _("non-numeric argument to function")); # https://github.com/r-devel/r-svn/blob/49597237842697595755415cf9147da26c8d1088/src/main/complex.c#L347 would become else errorcall(call, _("non-numeric argument to function [47]")); or else errorcall(call, _("[47] non-numeric argument to function")); Now the ID could be extracted more easily (at least for base R condition messages)... This would even be back-portable to older R versions to make condition IDs broadly available "in the wild". Another way to introduce an ID for each condition in base R would be ("the hard way") 1) by refactoring each and every code location with an embedded message string to use a centralized key/msg_text data structure to "look up" the appropriate message text and 2) use the key to enrich the condition as unique ID (e.g. as an attribute in the condition object).